OP wrote (a):
But doesn’t add-Migration has everything you need from DbContext and Configuration?
No - as mentioned above, the part of the manual migration code constructor (created by add-migration ) contains a snapshot of your database schema.
However, the fact that you are using a connection string, etc., is very strange. EF usually implies this from your DbContext and Web.Config classes (s). In a project where I have one database and one DbContext, I create a configuration class and add manual migration with:
add-migration
I do not need to pass any other command line arguments. What's in EF 4.3.1 - maybe you used CTP or some older version or just misunderstood the documents?
If I have several DBs or DbContexts, then I have several configuration classes and, for example, use:
add-migration -conf Log
Which uses my configuration class and associated connection string in Web.config to add manual migration for this database / DbContext.
Here's a longer sample simple DbContext code for storing logs (separate from the main db):
namespace MyProj.Models.Log { public class LogDb : DbContext { public DbSet<LogLine> LogLines { get; set; } public DbSet<LogTag> LogTags { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } } public LogDb() #if DEPLOYDB : base("LogDeploy") #else : base() #endif { } } namespace MyProj.Migrations { internal sealed class Log : DbMigrationsConfiguration<LogDb> { public Log() { AutomaticMigrationsEnabled = true; } } }
In Web.Config:
<add name="LogDb" connectionString="Initial Catalog=Log;Data Source=.\SqlExpress;Integrated Security=SSPI;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" /> <add name="LogDeploy" connectionString="Initial Catalog=Log;Data Source=00.00.000.00,12345;User ID=sql;Password=xxx;Network Library=DBMSSOCN" providerName="System.Data.SqlClient" />
So, in this example, I have several databases, several DbContexts. LogDb uses a different connection string in Web.Config based on the definition of "DBDEPLOY" at compile time; if so, it uses "LogDeploy". If not, it uses the default value - the connection string with the same name as the class, "LogDb". This allows me to easily deploy database changes to the server from my local computer, switching my project configuration, opening the port on the SQL db machine and running:
> update-database -conf Log
in the package manager console.