无法使用Code First Entity Framework 6.1更新数据库

时间:2014-05-28 23:00:06

标签: entity-framework

在我的项目中,我首先使用代码,然后我就可以创建数据库并通过nuget包管理器控制台将迁移应用到它。

现在我向我的应用程序添加了第二个数据库上下文,这个上下文不是代码优先。此上下文建立在现有数据库上。

添加第二个上下文后......如果我尝试更新数据库,我会收到错误

Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
System.NotSupportedException: Creating a DbModelBuilder or writing the EDMX from a DbContext created using Database First or Model First is not supported. EDMX can only be obtained from a Code First DbContext created without using an existing DbCompiledModel.
   at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
   at System.Data.Entity.Utilities.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
   at System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action`1 writeXml)
   at System.Data.Entity.Utilities.DbContextExtensions.GetModel(DbContext context)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()
   at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()

但是,我只想将迁移应用到我的第一个上下文中,并希望保留第二个上下文。

1 个答案:

答案 0 :(得分:0)

通过直接指定要更新的配置类型来解决问题

update-database -ConfigurationTypeName Company.Project.Migrations.MySpecificConfiguration

这篇文章有很多帮助

http://www.dotnet-tricks.com/Tutorial/entityframework/2VOa140214-Entity-Framework-6-Code-First-Migrations-with-Multiple-Data-Contexts.html