Entity Framework 4.1 禁用模型兼容性检查
我需要使用“DropCreateDatabaseIfModelChanges”-初始化程序类,因为我想创建一个特殊实体(表)(如果它不存在)。我的问题是,我的 DbContext 中还有另一个实体,它不应该成为模型兼容性检查的一部分。
我收到以下错误消息:
Model compatibility cannot be checked because the EdmMetadata type was not inclu
ded in the model. Ensure that IncludeMetadataConvention has been added to the Db
ModelBuilder conventions.
是否有可能从此检查中排除特殊实体?
编辑: 我已经按照德瓦特的建议做了。这个问题似乎与我最初想到的不同。如果我让 EF 使用我的 CheckedContext 创建一个新数据库,一切都会正常。但是当我尝试使用应该使用现有表的 NonCheckedContext 时,我收到了上面的错误消息...
I need to use an 'DropCreateDatabaseIfModelChanges' - Initializer class, because I want to create one special entity (table), if it doesn't exist. My problem is, that I've also got another entity in my DbContext, which shouldn't be part of the model compatibility check.
I'm getting the following error message:
Model compatibility cannot be checked because the EdmMetadata type was not inclu
ded in the model. Ensure that IncludeMetadataConvention has been added to the Db
ModelBuilder conventions.
Is there any possibility to exclude a special entity from this check?
EDIT:
I've done what Devart has suggested. The problem seems to be different, than I first tought. It all works fine, if I let EF create a new database with my CheckedContext. But I'm getting the error message above, when I'll try to use my NonCheckedContext wich should use an existing table ...
EDIT2:
This is a working solution. Everything works fine, when the database doesn't exist before. But it's no option for me, to Drop/Create the database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种可能的解决方案:创建一个继承自 DbContext 的上下文类,然后创建两个继承自基类的独立子上下文 - CheckedContext 和 NonCheckedContext,并相应地设置数据库初始化策略。
请注意,您应该首先访问 CheckedContext,以便它触发所有检查。
A possible solution: create a context class inherited from DbContext, and then create two separate subcontexts inherited from the base one - CheckedContext and NonCheckedContext, and set the Database Initialization Strategy accordingly.
Please note that you should access the CheckedContext first so that it fires all its checks.