Entity Framework 6 Code First Trigger

前端 未结 3 715
醉酒成梦
醉酒成梦 2020-12-09 16:03

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 ed

相关标签:
3条回答
  • 2020-12-09 16:11

    Check out my library EntityFramework.Triggers. It works at the Entity Framework layer, so the trigger events won't fire if someone modifies the database directly. The NuGet link is https://www.nuget.org/packages/EntityFramework.Triggers/

    0 讨论(0)
  • 2020-12-09 16:29

    After you add a migration, open the migration file and create your trigger as shown below

    Note: you need to run update-database to see the changes in your database.

    0 讨论(0)
  • 2020-12-09 16:32

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

    0 讨论(0)
提交回复
热议问题