Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 6 Code First Trigger

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?

like image 826
Charles W Avatar asked Feb 21 '14 19:02

Charles W


People also ask

How do I use code first in Entity Framework?

Step 1 − First, create the console application from File → New → Project… Step 2 − Select Windows from the left pane and Console Application from the template pane. Step 3 − Enter EFCodeFirstDemo as the name and select OK. Step 4 − Right-click on your project in the solution explorer and select Manage NuGet Packages…

How do I use triggers in Entity Framework?

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). Take note of his warning, however, EF will not be aware of any changes made in the trigger.


1 Answers

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).

like image 110
Erik Funkenbusch Avatar answered Sep 22 '22 11:09

Erik Funkenbusch