简体   繁体   English

如何在 Entity Framework Core 中模拟 SQL 触发器(代码优先)?

[英]How to emulate a SQL trigger in Entity Framework Core (code first)?

Entity Framework does not support triggers.实体框架不支持触发器。 Is there something that reproduces their behavior?有什么东西可以复制他们的行为吗?

I'm not interested in inserting SQL queries in my code.我对在我的代码中插入 SQL 查询不感兴趣。

I have this question because I have a case which I want to create a new table on my database that will be filled with information whenever something is inserted in another existing table.我有这个问题,因为我有一个案例,我想在我的数据库中创建一个新表,只要在另一个现有表中插入内容,该表就会填充信息。

Like this:像这样:

  • Suppose I have a Prodcuts table.假设我有一个Prodcuts表。 I want to create the ProductsStatus table.我想创建ProductsStatus表。

  • If something is inserted into the Products table, I want to add a record to the ProductStatus table, storing the ProductId (foreign key), a DateTime column and a Stored status for this product.如果将某些内容插入到Products表中,我想向ProductStatus表中添加一条记录,存储该ProductId (外键)、一个DateTime列和一个Stored状态。

  • The point is to avoid refactoring every place where I add a product on my code in order to do this new operation.关键是要避免重构我在代码上添加产品的每个地方,以便执行此新操作。

I do something like this in some of my contexts:我在某些情况下会做这样的事情:

public partial class XContext : DbContext
{

    private void TrackDates()
    {
        foreach (var entityEntry in ChangeTracker.Entries())
        {
            if (entityEntry.Entity is BaseEntity be)
            {
                switch (entityEntry.State)
                {
                    case EntityState.Deleted: //soft delete instead
                        entityEntry.State = EntityState.Modified;
                        be.DeletedDate = DateTime.UtcNow;
                        break;
                    case EntityState.Modified:
                        be.ModifiedDate = DateTime.UtcNow;
                        be.CreatedDate = DateTime.UtcNow;
                        break;
                    case EntityState.Added:
                        be.CreatedDate = DateTime.UtcNow;
                        break;
                }
            }
        }
    }

    public override int SaveChanges()
    {
        TrackDates();
        return base.SaveChanges();
    }
    public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
    {
        TrackDates();
        return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
    }
}

Every time SaveChanges is called it updates the UpdatedDate, or marks an DeletedDate to make soft delete etc, which is "kind of like what some triggers do sometimes".. Perhaps you can extend it to do some of your needs..每次调用 SaveChanges 时,它都会更新 UpdatedDate,或者标记一个 DeletedDate 以进行软删除等,这“有点像某些触发器有时会做的事情”.. 也许您可以扩展它来满足您的一些需求..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架6代码优先触发器 - Entity Framework 6 Code First Trigger Entity Framework Core 3 模拟 PARTITION BY - Entity Framework Core 3 emulate PARTITION BY 如何首先通过带有条件(字符串+自动递增编号)的ENTITY FRAMEWORK CORE代码在MS SQL中创建COMPUTED列 - How to create COMPUTED column in MS SQL by ENTITY FRAMEWORK CORE code first with condition (string + auto increment number) 如何使用 Entity Framework Core 和 Database First 在 MacOS 上连接到 SQL 数据库 - How to connect to a SQL database on MacOS with Entity Framework Core and Database First 实体框架核心如何首先实现代码? - How does Entity Framework Core implement code first? 实体框架如何使用代码优先方法生成此SQL代码? - Entity Framework how to produce this SQL code with code first approach? 实体框架核心关闭表(代码优先) - Entity framework core closure table (Code First) 实体框架核心2-代码首先不创建表 - Entity Framework Core 2 - Code first not creating tables 如何使用实体框架生成与SQL Server兼容的数据库(代码优先) - How to generate a SQL Server compatible database with Entity Framework (code first) 如何让 Entity Framework Code First 使用 SQL Azure DB? - How to make Entity Framework Code First use the SQL Azure DB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM