OP wrote:
But doesn't Add-Migration have everything it needs from the DbContext
and the Configuration?
No - as others mentioned here the Designer portion of the code for a Manual Migration (what's created by add-migration
) contains a snapshot of your database schema.
That said, the fact you're using a connection string etc is very odd. EF normally implies it from your DbContext class(es) and Web.Config. In a project where I have a single database and a single DbContext, I create a Configuration class and add a manual migration with:
add-migration
I don't have to pass any other command line arguments. That's in EF 4.3.1 - perhaps you were using a CTP or some older version, or just misunderstood the docs?
If I have multiple DBs or DbContexts, then I have multiple Configuration classes and use for example:
add-migration -conf Log
Which uses my Configuration class and related connection string in Web.config to add a manual migration for that database/DbContext.
Here's a longer code example of a simple DbContext meant 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 multiple databases, multiple DbContexts. The LogDb uses a different connection string in Web.Config based on whether "DBDEPLOY" is defined at compile time; if it is, it uses "LogDeploy." If not, it uses the default - the connection string with the same name as the class, "LogDb." This allows me to easily deploy DB changes to a server from my local machine, by switching my Project Configuration, opening a port on the SQL db machine, and running:
> update-database -conf Log
in the Package Manager Console.