Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify SQL Server File Locations when using Entity Framework Database.Create()

I have several Entity Framework Code First DbContext objects that use a custom Initializer.

In the initializer, the call to

context.Database.Create();

creates the database in SQL Server.

The Data and Log files are created in directories per the Database Settings in SQL Server.

I would like different DbContext subclasses to have different Data and Log file paths. Can I specify the paths somehow when creating the database, or must I detach/move/attach in a separate step after the database has been created?

like image 232
Eric J. Avatar asked Jun 28 '12 04:06

Eric J.


People also ask

How do I change the database connection in Entity Framework?

If you want to change the connection string go to the app. config and remove all the connection strings. Now go to the edmx, right click on the designer surface, select Update model from database, choose the connection string from the dropdown, Click next, Add or Refresh (select what you want) and finish.

Does Entity Framework create the database?

However, with ASP.NET you can use Entity Framework to generate a database based on an object model. Generally you would create an ER diagram and manually create all the tables one by one. With Entity Framework, you can create your entities in Visual Studio and all your tables will be created for you.


1 Answers

You can always use the AttachDBFilename keyword in the connection string. See the documentation of SqlConnection.ConectionString for more information.

like image 108
bricelam Avatar answered Oct 14 '22 07:10

bricelam