实体框架6码第一个触发器 [英] Entity Framework 6 Code First Trigger
问题描述
我使用实体框架六典首先,想创建一个触发器。
I'm using Entity Framework 6 Code First, and would like to create a Trigger.
我如何做到这一点?
我需要的触发器是因为用户可以直接或通过一个程序我写编辑数据库,我需要在一个表中确认2列的原因是不能同时为空,并且不能同时不为null。
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).
您可以使用拉吉斯拉夫这里指定的技术 EF 4.1代码第一次添加触发器到表
You can use the technique specified by Ladislav here EF 4.1 code-first adding a trigger to a table
注意到他的警告,但是,EF不会知道在触发所做的任何更改。如果你的目的仅仅是为了确保2列在一个表中都没有空,你会更好地约束(约束,也不受EF的支持,但您可以手动添加它们)担任。
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屋!