As an explicit database name when using Entity Framework Migrations 4.3

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

My connection string:

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

The first time I run Update-Database, my database is created with the correct name TestDB. However, as soon as I make changes to one of my entities, it will no longer be updated for me unless I add the name of the launch project (I use a solution for several projects):

 Update-Database -StartUpProjectName "TestDB.Data" 

Then another new database is created, the migration of which will always continue to be used. I don't mind inserting the StartUpProjectName command, but is there a way to override the default name for the database to be created? It always creates a database as

 TestDB.Data.DataContext 

Is there a way to ensure that the database created by passing the StartUpProject name is simply called TestDB or is it a limitation on the use of the StartUpProjectName parameter?

As a side note, I think the reason I need to specify StartUpProjectName is because I have a multi-level project setup. The Migrations configuration file is in my Data project, objects / models are in my Domain project, etc. I also do not have the initialization parameters in the Global.asax.cs file, as I would use the ef 4.2 code first. So in my project, I just have a DataContext in my Data project and Migrations Configuration in this project.

EDIT:

Since I originally asked this question, I came across the β€œright” way to name the database in a multi-project solution. Although the answer below will work, it means that you are duplicating your web.config in a different area, which is not an ideal solution. Instead, you can simply put the name in 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(); } } 

Thank,

Rich

+45
entity-framework ef-migrations
Feb 17 '12 at 11:27
source share
4 answers

When you run update-database you must specify the project containing the migrations. Make sure you have the app.config file in this project that contains the correct connection string.

When splitting an application into several projects, the connection string used when starting the application is one of the running projects. When migrating, the connection string used is used - this is one of the projects containing migrations.

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

+27
Feb 25 2018-12-12T00:
source share

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

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

Easy to drink if you like to print endlessly.

+61
May 23 '12 at 5:06 PM
source share

You may have a connection string stored in the web.config file in your site’s project, as well as DBContext and migration files in another project and still use the same connection string. However, you need to make sure that, as with the Data project (or any other project with DBContext, etc.) As the default project for the package manager console, you MUST also be sure that your website is set to StartUp project by default!

I don’t see this documented anywhere, but the crazy 24 hours of inability to understand why my migrations, which suddenly apply to SQLExpress db, led me to this conclusion.

+20
Dec 10 '13 at 15:39
source share

I tried with the latest EF5 from Nuget.

However, Update-Database does not read App.config from a project that contains migrations (just like the answer 1 year ago), but it will read *.config from the project start . This is great, but I will learn how Add-Migration and Update-Database find the appropriate connection string here:

  • First tries to get the default connection string "DefaultConnection"
  • Then it tries to get the name of the connection string based on the name of the context class. For example. I have a MyContext class derived from DbContext , so I can use the connection string name "MyContext". Useful when I have multiple db connections.
  • If both of the specified connection string names are not found, it will not work and will not show the connection string "DefaultConnection" unless you specify the -ConnectionStringName parameter. See get-help Update-Database for a help page in the package manager console.

There is no attempt to retry or return, so if "DefaultConnection" contains the wrong connection string, it will simply show an error.

If both DefaultConnection and the context name exist in the connection strings, DefaultConnection will take precedence.

I would prefer # 2 to be the first attempt, because the name is more specific, but the above steps are what EF5 Migrations do when trying to connect to db.

+8
Jan 14 '13 at 7:19
source share



All Articles