.NET Framework, you can use |DataDirectory| to configure connection string when connecting to SQL Server database file via attach mode:

AttachDbFilename=|DataDirectory|\dbname.mdf

In .NET Core, you cannot directly set SQL Server Express connection string by using any tokens directly. There is a workaround that can be used to get it work.

Connection string configuration in appSetting.json

"ConnectionStrings": {
     "KontextCoreConnection": "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=%CONTENTROOTPATH%\\App_Data\\kontext-docu.mdf;Integrated Security=True;Connect Timeout=30",
     "KontextDocuConnection": "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=%CONTENTROOTPATH%\\App_Data\\kontext-docu.mdf;Integrated Security=True;Connect Timeout=30"
   }

The above code snippet configures two connection strings with a token %CONTENTROOTPATH%.

Parse the tokens when reading connection strings

Based on the token used in the configurations, replace it with the actual content root path.

// Replace connection string tokens
             var connStrCore = Configuration.GetConnectionString(Constants.KontextCoreConnectionName);
             var connStrDocu = Configuration.GetConnectionString(Constants.KontextDocuConnectionName);
             if (connStrCore.Contains(Constants.ContentRootPathToken))
                 connStrCore = connStrCore.Replace(Constants.ContentRootPathToken, Env.ContentRootPath);
             if (connStrDocu.Contains(Constants.ContentRootPathToken))
                 connStrDocu = connStrDocu.Replace(Constants.ContentRootPathToken, Env.ContentRootPath);

            // Add database context
             services.AddDbContext<ApplicationDbContext>(options =>
             {
                 if (dbConfig.CoreDbType == DatabaseType.SQLite)
                 {
                     options.UseSqlite(connStrCore,
                         b => b.MigrationsAssembly("Kontext.Docu.Web.Portals"));
                 }
                 else
                 {
                     options.UseSqlServer(connStrDocu,
                         b => b.MigrationsAssembly("Kontext.Docu.Web.Portals"));
                 }
                 // Register context data models.
                 options.UseContextProjectShared();
             });

In Constants class, a static field is defined to indicate the replacement token.

public readonly static string ContentRootPathToken = "%CONTENTROOTPATH%";

Of course, you can change the value to any token you like as long as it is consistent with your configuration file.

info Last modified by Raymond at 2 years ago * This page is subject to Site terms.

More from Kontext

Machine Learning with .NET in Jupyter Notebooks

local_offer plot local_offer machine-learning local_offer jupyter-notebook local_offer C# local_offer dotnet core

visibility 473
thumb_up 0
access_time 7 months ago

In this article, I'm going to show you how to install Jupyter in Windows and then install .NET kernel for Jupyter notebooks. It also shows a machine learning example using ML.NET. The target audience are .NET developers who want to expand their skills in data engineering and science domain...

open_in_new .NET Machine Learning

local_offer spark local_offer pyspark

visibility 3903
thumb_up 0
access_time 13 months ago

When creating Spark date frame using schemas, you may encounter errors about “field **: **Type can not accept object ** in type <class '*'>”. The actual error can vary, for instances, the following are some examples: field xxx: BooleanType can not accept object 100 in type ...

open_in_new Spark + PySpark

local_offer hive

visibility 1663
thumb_up 0
access_time 2 years ago

Since Hive 3.x, new authentication feature for HiveServer2 client is added. When starting HiveServer2 service (Hive version 3.0.0), you may encounter errors like: ‘HiveServer2 metastore.RetryingMetaStoreClient: RetryingMetaStoreClient trying reconnect as [username]  (auth:S...

open_in_new Hadoop

local_offer linux local_offer WSL local_offer ubuntu

visibility 5673
thumb_up 3
access_time 2 years ago

This page shows how to install Windows Subsystem for Linux (WSL) system on a non-system drive manually. Enable Windows Subsystem for Linux system feature Open PowerShell as Administrator and run the following command to enable WSL feature: Enable-WindowsOptionalFea...

open_in_new Tools

info About author

comment Comments (0)

comment Add comment

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

No comments yet.

Dark theme mode

Dark theme mode is available on Kontext.

Learn more arrow_forward

Kontext Column

Created for everyone to publish data, programming and cloud related articles. Follow three steps to create your columns.


Learn more arrow_forward