EF6 Code first“model backing DataContext has changed”错误(.mdf Db) [英] EF6 Code first "model backing DataContext has changed" error (.mdf Db)

查看:145
本文介绍了EF6 Code first“model backing DataContext has changed”错误(.mdf Db)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我收到这个错误:



自从创建数据库以来,支持DataContext上下文的模型已经改变,考虑使用代码优先迁移来更新数据库。



我有一个使用EF6的WPF应用程序&安培; MVVM针对本地db(.mdf生活)运行。



我创建了一个新的模型,称为langauges,然后我添加了一个新的DbSet langauges> collection to my datacontext。



当代码尝试实例化一个数据文本对象时,我收到上面的错误。


$ b $我缺少什么?对于我所有其他新的表/模型,我可以在代码中创建它们,运行一个测试方法来在新的表/模型中创建一个记录,然后EF代码首先会自动创建新的表,并且通过抱怨来添加该项。为什么现在不工作?

解决方案

默认情况下,EF6代码首先不会重新创建数据库。在例外情况下这样说。它也带来了解决您的问题。您需要使用代码优先迁移



打开您的PackageManager控制台并输入以下


    启用迁移
  1. 添加迁移{名称}(这将创建一个带有
    更改的类对于第一个,我总是使用Init作为名称,因此很容易

  2. 更新数据库(这将更新您的数据库)

有关详细信息,请参阅 MSDN


I'm new to the code first methodology.

I'm getting this error:

"The model backing the 'DataContext' context has changed since the database was created. Consider using Code First Migrations to update the database".

I have a WPF app using EF6 & MVVM running against a local db (.mdf life).

I created a new model, called langauges then I added a new DbSet< langauges > collection to my datacontext.

When the code tries to instantiate the a datacontext object, I get the error above.

What am I missing? For all my other new tables/models I could just create them in code, run a test method to create a record in the new table/model and then EF code first would automatically create the new table and add the entry with complaining. Why won't it work now?

解决方案

By default EF6 code first will not recreate your database. It says so in the exception. It also comes with the solution to your problem. You need to use Code First Migrations

Open your PackageManager Console and type in the following

  1. Enable-Migrations
  2. Add-Migration {Name} (this will create a class with the changes For the first I always use Init as name so it is easy to go back to the initial state)
  3. Update-Database (this will update your database)

More information can be found on MSDN

这篇关于EF6 Code first“model backing DataContext has changed”错误(.mdf Db)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆