If I'm using Entity Framework 5 with LocalDb, is there a way of specifying the filename of the database in the app.config/web.config file?
The system database files for the database are stored in the local AppData path, which is normally hidden. For example, C:\Users\<user>\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\LocalDBApp1\ .
On further investigation it looks like it is really simple, but isn't clear when reading the docs.
First of all you need to have the entity framework part of the configuration
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
Once you have that, you then need to specify your connection string. By default the connection string name is the fully qualified name of your context. So in my test app, the context was called 'DataModel.Context', so I need a connection string for 'DataModel.Context'
<connectionStrings>
<add name="DataModel.Context" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=database;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\database.mdf" providerName="System.Data.SqlClient" />
This then uses the file 'database.mdf' in the data directory of the project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With