.net 使用 Entity Framework Code First Migrations 添加数据库触发器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21731889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add database trigger with Entity Framework Code First Migrations
提问by StefanG
I use Entity Framework Migrations for code first to control my database model. It works charming and I can handle until now everything. But now I need to add one database trigger and I would like to do this with EF Migrations and not use a separate sql script for only this case (This would be confusing for clients, esp. after we convinced them that we can handle everything with EF Migrations). My trigger is straight forward and looks like tis:
我首先使用实体框架迁移代码来控制我的数据库模型。它很有魅力,到目前为止我可以处理所有事情。但现在我需要添加一个数据库触发器,我想用 EF 迁移来做到这一点,而不是只在这种情况下使用单独的 sql 脚本(这会让客户感到困惑,尤其是在我们说服他们我们可以用EF 迁移)。我的触发器是直截了当的,看起来像 tis:
CREATE OR REPLACE TRIGGER [name] BEFORE UPDATE ON myTable ...
Is there a command to add a trigger to EF Migrations?
是否有向 EF 迁移添加触发器的命令?
回答by Andy Brown
You can just add a Sql("SQL COMMAND HERE")method call to your migration's Upmethod. Don't forget to also add the drop statement to the Downmethod. You can create an empty migration if you need, just by running Add-Migrationwithout any changes to the model.
您只需Sql("SQL COMMAND HERE")向迁移的Up方法添加一个方法调用即可。不要忘记还将 drop 语句添加到Down方法中。如果需要,您可以创建一个空的迁移,只需在Add-Migration不对模型进行任何更改的情况下运行即可。
public partial class Example : DbMigration
{
public override void Up()
{
Sql("CREATE OR REPLACE TRIGGER [name] BEFORE UPDATE ON myTable ...");
}
public override void Down()
{
Sql("DROP TRIGGER [name]");
}
}

