EF6 Code first “model backing DataContext has changed” error (.mdf Db)

不想你离开。 提交于 2019-12-10 11:45:22

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/22096614/ef6-code-first-model-backing-datacontext-has-changed-error-mdf-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!