如何检查数据库模式是否符合Entity Framework架构? [英] How to check if database schema matches Entity Framework schema?

查看:139
本文介绍了如何检查数据库模式是否符合Entity Framework架构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

令我感到惊讶的是,使用 CreateDatabaseIfNotExists 上下文初始值设置,

For my surprise, using the CreateDatabaseIfNotExists context initializer, the line

context.Database.Initialize(true)

如果模式不符合我的代码第一个模式。

doesn't throw an exception if the schema does not match my code first schema.

有没有办法验证当前数据库是否匹配我们的模式,例如,我们尝试访问一个实体,其表不不存在于数据库中,EF会抛出异常?

Is there a way to validate if the current database matches our schema before, for instance, we try to access a entity, whose table doesn't exist on the database anymore, and an exception is thrown by EF?

推荐答案

您可以调用CompatibleWithModel来确定数据库匹配模型。如果将参数设置为true,则在数据库中没有找到模型数据时将抛出异常。

You can call CompatibleWithModel to determine if the database matches the model. If you set the parameter to true it will throw an exception if no model data is found in the database.

bool isCompatible = context.Database.CompatibleWithModel(true);

这篇关于如何检查数据库模式是否符合Entity Framework架构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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