Add database trigger with Entity Framework Code First Migrations
.net, entity-framework, entity-framework-migrations
Solution
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]");
}
}
Problem
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: ``` CREATE OR REPLACE TRIGGER [name] BEFORE UPDATE ON myTable ... ``` Is there a command to add a trigger to EF Migrations?