1

Entity Framework does not support triggers. Is there something that reproduces their behavior?

I'm not interested in inserting SQL queries in my code.

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. I want to create the ProductsStatus table.

  • 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.

  • The point is to avoid refactoring every place where I add a product on my code in order to do this new operation.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ramon Dias
  • 835
  • 2
  • 12
  • 23

1 Answers1

1

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..

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • thanks for the answer. in my case, it did not worked. It's raising `System.InvalidOperationException: Collection was modified; enumeration operation may not execute.` when iterating the foreach loop. – Ramon Dias Oct 27 '20 at 13:55
  • Did you attempt to add or remove objects from a collection while you were enumerating it? Don't. Add those things to another thing, like a list, then when you're done enumerating, add the contents of the list to the collection you want to modify. It's a standard .net thing; you can't saw off the branch you're currently sitting in – Caius Jard Oct 27 '20 at 16:08