How to get the name of the database?

Vote:
 

Hi!

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:01
Vote:
 

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

#200445
Jan 11, 2019 21:12
Vote:
 

The connections.config file has the following strings:

<connectionStrings>
<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" />
</connectionStrings>

referenced in this part of the web.config:

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

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
Vote:
 

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
Vote:
 

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:26
Vote:
 
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):

<episerver.dataStore>
    <dataSettings connectionStringName="ThisIsMyConnectionName" />
</episerver.dataStore>

but default one is "EPiServerDB"

#200552
Jan 15, 2019 17:07
Vote:
 

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

#200553
Jan 15, 2019 17:08
Vote:
 

Thank you!

#200571
Jan 16, 2019 12:42
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.