是否可以使用Entity Framework Core创建SQL触发器。
也许通过使用
中的说明protected override void OnModelCreating(DbModelBuilder dbModelBuilder)
{
}
或者只是通过在迁移脚本中执行SQL语句
public override void Up()
{
}
答案 0 :(得分:3)
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(@"create trigger .....");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(@"drop trigger <triggerName>");
}
答案 1 :(得分:1)
Laraue.EfCoreTriggers软件包,用于通过流利的语法创建SQL触发器,该触发器允许在触发器如此工作后执行插入,更新,upsert,删除,如果不存在则插入语句
modelBuilder.Entity<Transaction>()
.AfterInsert(trigger => trigger
.Action(triggerAction => triggerAction
.Upsert(transaction => new { transaction.UserId },
insertedTransaction => new UserBalance { UserId = transaction.UserId, Balance = insertedTransaction.Sum },
(insertedTransaction, oldBalance) => new UserBalance { Balance = oldBalance.Balance + insertedTransaction.Sum })));
此代码将转换为sql并应用于使用的迁移
migrationBuilder.Sql()