检查约束实体框架 [英] check constraint entity framework

查看:146
本文介绍了检查约束实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

EF目前不支持检查约束。



也就是说,您不能将属性应用于实体,期望它生成适当的SQL以在底层数据库中创建检查约束。



使用代码优先数据迁移时的一种方法( http://blogs.msdn .com / b / adonet / archive / 2012/02/09 / ef-4-3-code-based-migrations-walkthrough.aspx )是生成迁移并添加一个 Sql () Up()中生成的函数覆盖



eg

  public override void Up()
{
//自动生成ed code here
// ...

//您的自定义SQL GOES HERE
Sql(ALTER TABLE foo ADD CONSTRAINT CK_Some_Check CHECK(SomeDate> '20121110 00:00:00.000'); **
}

另见问题和答案的一些进一步的信息:
是否可以表示检查约束?


IN EF can I add a check constraint so if my CustomerNotes entity has a boolean "FollowUpRequired" property I force user to enter a valid future date in the "FollowUpDate" property?

解决方案

EF does not currently support Check constraints.

That is, you cannot apply an attribute to an entity, and expect it to generate the appropriate SQL to create the Check constrint in the underlying database.

One way around this when using Code First Data Migrations (http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx) is to generate a migration and add an Sql() line in the Up() function override that is generated

e.g.

public override void Up()
{
    // Auto generated code here
    // ...

    // YOUR CUSTOM SQL GOES HERE
    Sql("ALTER TABLE foo ADD CONSTRAINT CK_Some_Check CHECK (SomeDate > '20121110 00:00:00.000'");**
}

Also see this question and answer for some further information: Is it possible to express a check constraint?

这篇关于检查约束实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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