Entity Framework model change error

你离开我真会死。 提交于 2019-12-10 02:50:24

问题


I am getting the error

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

I am using Entity Framework and have changed my model after declaring DataContext. How can I fix this error?


回答1:


  1. If you already deployed your application or you don't want remove data from database you must read about Code First Migrations. Here you have a link: http://msdn.microsoft.com/en-us/library/hh770484(v=vs.103).aspx

  2. If you can delete database just do it. EF will create new database that match your model.

  3. You can also disable creating/updating database structure by invoking the following code:

    Database.SetInitializer<MyDbContext>(null);
    



回答2:


If you delete the __MigrationHistory table in SQL Server it should fix it.




回答3:


I used database first to create a project after I had changed my database context and solved my problem:

Database.SetInitializer<Models.YourDbContext>(null);

Don't forget to handle the DbUpdateException




回答4:


Delete the __MigrationHistory table in SQL Server or just or all rows of this table it should fix it



来源:https://stackoverflow.com/questions/17010190/entity-framework-model-change-error

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