Add database trigger with Entity Framework Code First Migrations

后端 未结 2 961
日久生厌
日久生厌 2020-12-14 03:04

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 tr

2条回答
  •  既然无缘
    2020-12-14 03:20

    You can just add a Sql("SQL COMMAND HERE") method call to your migration's Up method. Don't forget to also add the drop statement to the Down method. You can create an empty migration if you need, just by running Add-Migration without any changes to the model.

    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]");
        }
    }
    

提交回复
热议问题