How can I disable model compatibility checking in Entity Framework 4.3?

妖精的绣舞 提交于 2019-11-26 12:46:37

问题


I\'m working with EF 4.3 and have a context which needs to talk to a database which was generated by another library using EF Code First 4.3. The context is throwing an exception stating

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

In EF 4.1 this could be diabled by removing the IncludeMetadataConvention from the ModelBuilder. However, in 4.3 this convention has been deprecated and no longer has an effect.

How can I have an EF 4.3 context talk to an EF 4.3-generated database built by a different context? The only option I\'ve found (which is far from ideal) is to delete the metadata table, thereby causing both contexts to assume the database was not build by EF.

PS: I know this scenario is likely to raise questions about why I need to do this; I know it\'s far from ideal, but rest assured it\'s a problem I need to solve and have limited options to work with laterally.


回答1:


Setting the initializer to null will skip the model compatibility check.

Database.SetInitializer<MyContext>(null);



回答2:


For EF 4.3 or higher

Database.SetInitializer<MLTServerWatcherContext>(null);

Or if using older version of EF

modelBuilder.Conventions.Remove<IncludeMetadataConvention>();

(I know he said he are using EF 4.3, but i think it's good to show this option too)



来源:https://stackoverflow.com/questions/10623260/how-can-i-disable-model-compatibility-checking-in-entity-framework-4-3

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