Code First迁移更新localdb上的错误Db

时间:2014-07-29 11:02:46

标签: entity-framework ef-code-first code-first ef-migrations

我正在使用代码优先迁移来为我的asp mvc站点创建和更新数据库。

我有一个DbContext,它位于解决方案的另一个项目中。

public class EFDbContext : DbContext
{
    public DbSet<Book> Books { get; set; }
    public DbSet<Author> Authors { get; set; }
    public DbSet<Publisher> Publishers { get; set; }
}

当我启用迁移和添加迁移 [名称] 时。它似乎创建了自己的名为

的数据库
[foo-projectname].Domain.Concrete.EFDbContext

而不是附加到我创建的数据库,称为 [foo-projectname] 。 在网站的webconfig中,连接字符串按目录&#34; [foo-projectname] &#34;

引用。

如果我将webconfig更改为其他数据库,那么我将从已添加到该数据库中的项目中获取结果。但是,我想使用我创建的数据库。

我不想坚持使用这个自动创建的原因是因为我不确定迁移到SqlServer并且不想再陷入困境。我也收到了错误

  

支持&#39; EFDbContext&#39;自......以来情境发生了变化。

即使我没有改变任何东西。

1 个答案:

答案 0 :(得分:8)

X是包含派生DbContext类的项目名称。

  • 确保Default project上的Package Manager Console为X
  • 确保解决方案上的活动启动项目为X

另一种解决方法是,在运行迁移语法时提供以下参数。

-ProjectName X -StartUpProjectName X

一些参数说明:

-ProjectName <String>
    Specifies the project that contains the migration configuration type to be
    used. If omitted, the default project selected in package manager console
    is used.

-StartUpProjectName <String>
    Specifies the configuration file to use for named connection strings. If
    omitted, the specified project's configuration file is used.

-ConfigurationTypeName <String>
    Specifies the migrations configuration to use. If omitted, migrations will
    attempt to locate a single migrations configuration type in the target
    project.

-ConnectionStringName <String>
    Specifies the name of a connection string to use from the application's
    configuration file.

-ConnectionString <String>
    Specifies the the connection string to use. If omitted, the context's
    default connection will be used.