How to get the name of the database?

Wojciech Gabel
Member since: 2019


I need to get the main episerver database name to use with entity framework.

I know I could get it from System.Configuration.ConfigurationManager.ConnectionStrings but I will not have the name of the string so this is useless.

It could be retrieved from EPiServer.Data.Providers.IDatabaseFactory, but that interface is obsolete :(

Is there a built in method that will return the main episerver database name?

#200437 Edited, Jan 11, 2019 17:06
  • Quan Mai
    Member since: 2011

    Not sure what you meant but episerver cms connection string name is always EPiServerDB

    #200445 Jan 11, 2019 21:12
  • Wojciech Gabel
    Member since: 2019

    The connections.config file has the following strings:

    <add name="EPiServerDB" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|EPiServer.Cms.mdf;Initial Catalog=EPiServer.Cms;Connection Timeout=60;Integrated Security=True;MultipleActiveResultSets=True" />

    referenced in this part of the web.config:

    <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="EPiServerDB" applicationName="/" />

    The name of this string is EPiServerDB. But what when this is changed? Then my querry for the database name, using the connection string will result in an error.

    So how can I get the correct name of the database episerver uses, without specifing any name, that could potentialy be changed by a user somewhere down the line?

    #200487 Jan 14, 2019 12:13
  • Quan Mai
    Member since: 2011

    the connectionstring name is a const and will not be changed, because it will break all other parts in the system. 

    #200488 Jan 14, 2019 12:18
  • Wojciech Gabel
    Member since: 2019

    I am making a nuget package, that will save some things into episerver database.

    I dont know what is the name of the dabase, and I dont know what is the name of the connection string.

    I need to use the episerver database and cannot create a seperate database.

    I would prefer to use Entity Framework.

    It all boils down to " How to extract the name of the main episerver database without explicit names and magic strings."

    #200493 Edited, Jan 14, 2019 13:28
  • valdis iljuconoks
    Member since: 2011
    var epiDataOptions = context.Locate.Advanced.GetInstance<DataAccessOptions>();
    var connectionName = epiDataOptions.DefaultConnectionStringName;  // will return `EPiServer` or any other configured connection string name

    name of the connection string can be changed by adding following line in web.config file (or any other where you have <episerver.dataStore> section):

        <dataSettings connectionStringName="ThisIsMyConnectionName" />

    but default one is "EPiServerDB"

    #200552 Jan 15, 2019 17:07
  • valdis iljuconoks
    Member since: 2011

    `context.Locate.Advanced` is just a shortcut used in init modules to get to Service Locator

    #200553 Jan 15, 2019 17:08
  • Wojciech Gabel
    Member since: 2019

    Thank you!

    #200571 Jan 16, 2019 12:42