This site uses cookies to deliver our services. By using this site, you acknowledge that you have read and understand our Cookie and Privacy policy. Your use of Kontext website is subject to this policy. Allow Cookies and Dismiss

Set AttachDbFilename as Relative Path in .NET Core

20 views 0 comments last modified about 12 days ago Raymond Tang

dotnet core lite-log

.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.

Related pages

Instantiate a Service in ConfigureServices Method in .NET Core

14 views   0 comments last modified about 12 days ago

.NET Core is built in with dependency injection. Usually method ConfigureServices in Startup class is used to register services in the container. The signature of the method looks like the following: public void ConfigureServices(IServiceC...

View detail

Instantiate a Service in ConfigureServices Method in .NET Core

14 views   0 comments last modified about 12 days ago

.NET Core is built in with dependency injection. Usually method ConfigureServices in Startup class is used to register services in the container. The signature of the method looks like the following: public void ConfigureServices(IServiceC...

View detail

ASP.NET Core 2.1 Error - 'Cyrillic' is not a supported encoding name

36 views   0 comments last modified about 17 days ago

After upgrading to ASP.NET Core 2.1 (.NET Core SDK 2.1.301), you may encounter the following error about encoding: System.ArgumentException &nbsp;&nbsp; HResult=0x80070057 &nbsp;&nbsp; Message='Cyrillic' is not a supported encoding name. For information on defining a custo...

View detail

SQLite in .NET Core with Entity Framework Core

22 views   0 comments last modified about 20 days ago

SQLite is a self-contained and embedded SQL database engine. In .NET Core, Entity Framework Core provides APIs to work with SQLite. This page provides sample code to create a SQLite database using package Microsoft.EntityFrameworkCore.Sqlite . Create sample project ...

View detail

SQLite in .NET Core with Entity Framework Core

22 views   0 comments last modified about 20 days ago

SQLite is a self-contained and embedded SQL database engine. In .NET Core, Entity Framework Core provides APIs to work with SQLite. This page provides sample code to create a SQLite database using package Microsoft.EntityFrameworkCore.Sqlite . Create sample project ...

View detail

Graphics Programming and Image Processing in .NET Core 2.x

14 views   0 comments last modified about 22 days ago

In .NET Core 2.x, Windows Forms or WPF are not implemented since they are based on GDI+ and DirectX respectively in Windows. In .NET Core 3.0, there is plan to add Desktop Packs which includes UWP. WPF and Windows Forms. However, they will still be Windows-only. In .NET Core applications, you may...

View detail

Add comment

Please login first to add comments.  Log in New user?  Register

Comments (0)

No comments yet.