创建数据库后,支持“DataContext”上下文的模型已更改 [英] The model backing the 'DataContext' context has changed since the database was created
问题描述
我正在尝试使用代码优先与迁移。即使我的模型目前没有任何变化,我也有异常。当我添加一个迁移时,上下文是空的,但是我收到如下消息的运行时错误:
I am trying to use Code First with Migrations. Even though there are no current changes to my model, I'm getting an exception. When I add a migration, the up and down are empty, but I get a runtime error with the message as follows:
在
EntityFramework.dll中键入'System.InvalidOperationException',但未在用户代码中处理
An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code
附加信息:支持MyDataContext上下文$ b的模型$ b自数据库创建以来已更改。考虑使用Code First
迁移来更新数据库( http://go.microsoft.com/fwlink/ ?
Additional information: The model backing the 'MyDataContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?
我的架构如下:
- 包含上下文,流体配置和迁移代码的DataAccess项目
- 包含poco类的模型项目
- Web API和MVC项目每个都包含各自web.config文件中的连接字符串。
此外,我还有以下代码:
Additionally I have the following code:
DbInitializer
DbInitializer
public static MyDataContext Create()
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDataAccess.MyDataContext, MyDataAccess.Migrations.Configuration>());
return new MyDataContext(ConfigurationManager.ConnectionStrings["MyDataContext"].ConnectionString, null);
}
我使用AutomaticMigrationsEnabled = false;在迁移配置构造函数中,正如我所理解的那样,这将允许(并且要求)更好地控制迁移应用时的控制。我也尝试将其设置为true,但结果相同。
I started with AutomaticMigrationsEnabled = false; in the migration Configuration constructor, as it was my understanding that this would allow (and require) me to have more control over when migrations were applied. I have also tried setting this to true but with the same result.
我收到此错误时添加了新的迁移,Up方法为空。我将数据库更新为此新迁移,并在_migrationHistory表中创建了一条记录,但是当我尝试运行应用程序时仍然收到错误。另外,种子数据没有添加到数据库中。
I added a new migration upon receiving this error, and the Up method was empty. I updated the database to this new migration, and a record was created in the _migrationHistory table, but I still receive the error when I attempt to run the application. Also, the seed data was not added to the database.
protected override void Seed(MyDataAccess.MyDataContext context)
{
IdentityResult ir;
var appDbContext = new ApplicationDbContext();
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(appDbContext));
ir = roleManager.Create(new IdentityRole("Admin"));
ir = roleManager.Create(new IdentityRole("Active"));
ir = roleManager.Create(new IdentityRole("InActive"));
var userNamager = new UserManager<User>(new UserStore<User>(appDbContext));
// assign default admin
var admin = new User { UserName = "administrator", Email = "myAdmin@gmail.com" };
ir = userNamager.Create(admin, "myp@55word");
ir = userNamager.AddToRole(admin.Id, "Admin");
}
其中
public class ApplicationDbContext : IdentityDbContext<User>
{
public ApplicationDbContext()
: base("MyDataContext", throwIfV1Schema: false)
{
}
...
问题:如果Add-Migration在模型中没有看到任何更改,为什么在运行时会收到此错误?为什么种子代码没有被击中?如何解决这个问题,或者如果不能确定,我该如何进一步确定根本原因?
The question: If Add-Migration isn't seeing any change in the model, why do I get this error when I run? Why isn't the seed code being hit? How do I fix this, or if that can't be determined, how do I further determine the root cause?
推荐答案
为我工作。
转到软件包管理器控制台和运行 - 更新 - 数据库--force
Go to Package Manager Console and Run - Update-Database -force
这篇关于创建数据库后,支持“DataContext”上下文的模型已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!