I am trying to implement something like this accepted answer but my BaseEntity
is a generic class which looks like this:
public abstract class BaseEntity<TKey>
{
public TKey Id { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}
Now I want to implement Timestamp addition on the SaveChangesAsync()
method, but I have no idea how to go about doing that. My code looks like this:
...
var entries = ChangeTracker
.Entries()
.Where(e => e.Entity is BaseEntity && (
e.State == EntityState.Added
|| e.State == EntityState.Modified));
foreach (var entityEntry in entries)
{
((BaseEntity)entityEntry.Entity).UpdatedAt = DateTime.Now;
if (entityEntry.State == EntityState.Added)
{
((BaseEntity)entityEntry.Entity).CreatedAt = DateTime.Now;
}
}
but the issue is BaseEntity
is a generic type so the comparison doesn't work.