Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to explicitly name the database when using Entity Framework Migrations 4.3

I've recently started using Entity Framework migrations and noticed that the database name is not pulling through for me when I run the Update-Database command.

My connectionstring is:

<connectionStrings> <add name="DataContext" connectionString="Server=.\SQLEXPRESS;Initial Catalog=TestDB;Trusted_Connection=Yes;" providerName="System.Data.SqlClient" /> </connectionStrings> 

The very first time I run Update-Database my database is created with the correct name TestDB. However, as soon as I make a change to one of my entities it will not update any longer for me unless I add a Start Up Project Name (I'm using a multi project solution):

Update-Database -StartUpProjectName "TestDB.Data" 

This then makes another new database which migrations will always continue to use. I don't mind having to put in the StartUpProjectName command but is there a way to override the default name for the database this produces? It always creates the database as

TestDB.Data.DataContext 

Is there a way to ensure that the database created when passing the StartUpProject name is just called TestDB or is this a limitation of using the StartUpProjectName setting?

As a note, I think the reason I need to specify the StartUpProjectName is that I have a multilayer project setup. The Migrations Configuration file is in my 'Data' project, the entities/models are in my 'Domain' project, etc. I also do not currently have any initialize options in my Global.asax.cs file as I would have used previously on code first ef 4.2. So in my project I just have a DataContext in my Data project and the Migrations Configuration in that project also.

EDIT:

Since I originally setup this question I stumbled onto the 'correct' way to name a database in a multiproject solution. While the answer below will work it does mean you are duplicating your web.config in another area which isn't an ideal solution. Instead you can just put the name into your DbContext by doing something like this (DataContext is just the name I used in my project):

public class DataContext : DbContext {     public DataContext() : base("DatabaseNameHere")     { }      public DbSet<Table1> Table1 { get; set; }     public DbSet<Table2> Table2 { get; set; }      public virtual void Commit()     {         base.SaveChanges();     } } 

Thanks,

Rich

like image 217
Richard Reddy Avatar asked Feb 17 '12 11:02

Richard Reddy


People also ask

How do I use migrations in Entity Framework?

Following is the context class. Step 1 − Before running the application you need to enable migration. Step 2 − Open Package Manager Console from Tools → NuGet Package Manger → Package Manger Console. Step 3 − Migration is already enabled, now add migration in your application by executing the following command.

What is a database migration in Entity Framework EF core?

Migration is a way to keep the database schema in sync with the EF Core model by preserving data. As per the above figure, EF Core API builds the EF Core model from the domain (entity) classes and EF Core migrations will create or update the database schema based on the EF Core model.

What is migration history table in Entity Framework?

Migrations history table is a table used by Code First Migrations to store details about migrations applied to the database. By default the name of the table in the database is __MigrationHistory and it is created when applying the first migration to the database.

How do you add migration to a database?

For technical information, type: "get-help Add-Migration -full". After creating a migration file using the add-migration command, you have to update the database. Execute the Update-Database command to create or modify a database schema.


2 Answers

You can avoid managing it in app.config by offering it as a parameter:

Update-Database -Verbose   -ConnectionString "CONNECTIONSTRING"   -ConnectionProviderName "System.Data.SqlClient"  -StartupProjectName WEBSITE_PROJECT -ProjectName MIGRATION_PROJECT 

Easy-piezy, if you love to type endlessly.

like image 68
Scott Stafford Avatar answered Sep 23 '22 20:09

Scott Stafford


When doing update-database you should specify the project that contains the migrations. Make sure that you have an app.config file in that project that contains the correct connection string.

When splitting up an application over several projects, the connection string used when running the app is the one of the project started. When migrating, the connection string used is the one of the project containing the migrations.

When I did a similar setup I had to add the connection string in two places. A bit awkward, but it works.

like image 44
Anders Abel Avatar answered Sep 21 '22 20:09

Anders Abel