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
entity-framework ef-migrations
Richard Reddy Feb 17 '12 at 11:27 2012-02-17 11:27
source share