实体框架 6 代码优先触发器

Entity Framework 6 Code First Trigger(实体框架 6 代码优先触发器)

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

问题描述

我正在使用 Entity Framework 6 Code First,并希望创建一个触发器.

我该怎么做?

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

我一直在寻找,但找不到路.

有没有办法先用代码指定触发器?

解决方案

Entity Framework 不支持触​​发器,虽然你当然可以手动执行会创建触发器的语句,但是你需要在表格被创建(如果使用迁移).

您可以使用 Ladislav 在 EF 4.1 代码优先向表中添加触发器

请注意他的警告,但是,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 in 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 代码优先触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:实体框架 6 代码优先触发器

基础教程推荐