实体框架6代码第一触发器 [英] Entity Framework 6 Code First Trigger

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

问题描述



我如何做?



我需要触发器的原因是因为用户可以直接编辑数据库,也可以通过我正在编写的程序来编辑数据库,我需要确保表中的2列都不为空,而且不是都不是null。



我一直在寻找,找不到方法。



是否有任何方式首先使用代码指定触发器?

解决方案

实体框架不支持触发器,虽然您当然可以手动执行声明将创建一个触发器,但您需要在创建表(如果使用迁移)后执行此操作。



您可以使用Ladislav指定的技术<一个href =https://stackoverflow.com/questions/5913352/ef-4-1-code-first-adding-a-trigger-to-a-table> EF 4.1代码 - 首先向表中添加触发器



记笔记o然而,他的警告,EF不会意识到触发器发生的任何变化。如果您的意图仅仅是为了确保表中的2列不为空,您可以更好地使用约束(EF不支持约束,但您可以手动添加它们)。


I'm using Entity Framework 6 Code First, and would like to create a Trigger.

How do I do this?

The reason I need the trigger is because a user may either edit the database directly or through a program I'm writing, and I need to make sure 2 columns in a table are not both null, and are not both not null.

I've been looking and can't find a way.

Is there any way to specify a trigger using code first?

解决方案

Entity Framework has no support for triggers, although you can certainly manually execute a statement that would create a trigger, but you would need to do this after the table was created (if using migrations).

You can use the technique specified by Ladislav here EF 4.1 code-first adding a trigger to a table

Take note of his warning, however, EF will not be aware of any changes made in the trigger. If your intent is merely to ensure that 2 columns in a table are not null, you'd be better served with a constraint (constraints are also not supported by EF, but you can add them manually).

这篇关于实体框架6代码第一触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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