EF 5 +天青+ MigrationFirst覆盖数据库名称。为什么?

问题描述:

我创建了Azure中的数据库中设置自己的自定义名称。然后,我创建了EF 5 code第一实体和补充迁移。在应用程序启动时我称这些两行:

I created a database in Azure setting my own custom name. I then created EF 5 code first entities and added migrations. On application startup I called these two lines:

        Database.DefaultConnectionFactory = new SqlConnectionFactory(connectionString);
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDataContext, MyConfiguration>());

连接字符串直接取自Azure的:
服务器= TCP:xxx.database.windows.net,1433;数据库=数据库名;用户ID = YYY;密码= ZZZ; Trusted_Connection = FALSE;加密= TRUE;连接超时= 30;

Connection string is taken straight from Azure: Server=tcp:xxx.database.windows.net,1433;Database=dbName;User ID=yyy;Password=zzz;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;

在拳头叫我预计数据库数据库名称按照POCO模式充满表。
而是一个新的数据库与我的上下文的完整的命名空间名称产生:
MyService.Business.Entity.MyContext

On fist call I expected database dbName to be filled with tables according to POCO schema. But instead a NEW database is generated with the complete namespace name of my context: MyService.Business.Entity.MyContext

为什么会迁移不接受连接字符串中指定的数据库名称?

Why will the migration not accept the database name specified in the connection string?

您可以在您的DbContext的构造函数指定数据库名称或连接字符串名称:

You can specify the Database name or connection string name in the constructor of your DbContext:

public class MyDataContext : DbContext
{
    public MyDataContext: base("DbNameOrConntectionStringNameHere")
    {
    }
}