Kristoffer Lindén
Feb 7, 2022
  3909
(0 votes)

Attach local database using .NET Core and CMS 12

In my CMS 11 solution I used the variable |DataDirectory| to point out the App_Data folder where my database file is placed and that workd just fine. Is Net Core I tried to use the same:

"ConnectionStrings": {
    "EPiServerDB": "Data Source=(LocalDb)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\EPiServerDB_f6c7a112.mdf;Initial Catalog=EPiServerDB_f6c7a112;Connection Timeout=60;Integrated Security=True;MultipleActiveResultSets=True"
  }

In CMS 12 using Net Core, the |DataDictionary| does not work anymore and need to be replaced in some way. I found this that I thought was my solution:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    string baseDir = env.ContentRootPath;
    AppDomain.CurrentDomain.SetData("DataDirectory", System.IO.Path.Combine(baseDir, "App_Data"));
}

What happend was that when calling ConfigureCmsDefaults() which is done in Program.cs, Episerver tries to connect to the database but the connectionstring is not modified so that fails. A strange behavior was that the code then never reached the Configure(...) method and I could never understand why until I set the real physical path to the database in the connectionstring.

"ConnectionStrings": {
    "EPiServerDB": "Data Source=(LocalDb)\\MSSQLLocalDB;AttachDbFilename=D:\\Projekt\\Web\\App_Data\\EPiServerDB_f6c7a112.mdf;Initial Catalog=EPiServerDB_f6c7a112;Connection Timeout=60;Integrated Security=True;MultipleActiveResultSets=True"
  }

Then everything worked just fine, but is doesn't look to good and finally I found a solution using PostConfigure.

Connectionstring looks like this:

"ConnectionStrings": {
  "EPiServerDB": "Data Source=(LocalDb)\\MSSQLLocalDB;AttachDbFilename=App_Data\\EPiServerDB_f6c7a112.mdf;Initial Catalog=EPiServerDB_f6c7a112;Connection Timeout=60;Integrated Security=True;MultipleActiveResultSets=True",
}

And ConfigureServices like this:

public void ConfigureServices(IServiceCollection services)
{
        services.PostConfigure<DataAccessOptions>(o =>
        {
            o.SetConnectionString(_configuration.GetConnectionString("EPiServerDB").Replace("App_Data", Path.GetFullPath("App_Data")));
        });
        services.PostConfigure<ApplicationOptions>(o =>
        {
            o.ConnectionStringOptions.ConnectionString = _configuration.GetConnectionString("EPiServerDB").Replace("App_Data", Path.GetFullPath("App_Data"));
        });
}

In this way the connectionstring is modified before Episerver is initiated and the database will be attached correctly.

Feb 07, 2022

Comments

Vincent
Vincent Feb 8, 2022 12:24 AM

Hi Kristoffer

Thanks for sharing.

It's a bit strange you need to use PostConfigure for this. I don't find any issue by using Configure to replace the token. In addition, if your solution is CMS only, I found out there is DefaultConnectStringName property you can use instead of hard-coded EPiServerDB. The following snippet is my solution that I'm planning to post in my blog :)

My connectionstring in appsettings.Development.json

  "ConnectionStrings": {
    "EPiServerDB": "Server=(LocalDb)\\MSSQLLocalDB;AttachDbFilename=%CONTENTROOTPATH%\\APP_DATA\\foundation-mvc-net5.Cms.mdf;Initial Catalog=foundation-mvc-net5.Cms;Connection Timeout=60;Integrated Security=True;MultipleActiveResultSets=True"
  },

My startup ConfigureService class changes

 public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<DataAccessOptions>(o =>
            {
                o.ConnectionStrings.Add(new ConnectionStringOptions
                {
                    ConnectionString = _configuration.GetConnectionString(o.DefaultConnectionStringName).Replace(ContentRootPathToken, _webHostingEnvironment.ContentRootPath),
                    Name = o.DefaultConnectionStringName
                });
            });



            services.AddCmsAspNetIdentity<SiteUser>();
...........
}

Kristoffer Lindén
Kristoffer Lindén Feb 8, 2022 08:49 AM

Hi Vincent,

Thanks for the update. I have actually not tried the Configure, the solution I found used PostConfigure so that is the only option I have tried. I will try with Configure and let you know.

Johan Petersson
Johan Petersson Feb 8, 2022 09:10 AM

You can also remove MultipleActiveResultSets=True from the connection string if you're running latest version of CMS Core. You should see a warning in the logs if MARS is enabled but not needed.

Vincent
Vincent Feb 8, 2022 10:39 PM

Hi Johan

Interesting suggestion. Do you mean remove MAR setting for LocalDb scenario or any type of dbs? Any side effect with this?

Thanks

Vincent
Vincent Feb 9, 2022 02:10 AM

Hi Johan

In fact, the MARS setting seems required in this article Initial configuration | Optimizely Developer Community

Johan Petersson
Johan Petersson Feb 9, 2022 08:32 AM

The documentation needs to be updated. We only support SQL Server, so not any type of database. MARS causes issues on SQL Server on Linux. There's no issues having it enabled on Windows.

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024