Let's say I have some interface like:
public interface ISoftDeletable
{
bool IsActive { get; set }
}
And I have many entities that implement it:
public class Entity1 : ISoftDeletable
{
public int Id { get; set }
public bool IsActive { get; set; }
}
public class Entity2 : ISoftDeletable
{
public int Id { get; set }
public bool IsActive { get; set; }
}
In OnModelCreating
:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Entity1>().Property(e => e.IsActive).HasDefaultValue(true);
modelBuilder.Entity<Entity2>().Property(e => e.IsActive).HasDefaultValue(true);
}
Is there any way to refactor this so I can set HasDefaultValue
for all entities implementing ISoftDeletable
instead of doing this like above?
I can probably solve this specific case using a default constructor for each entity with IsActive = true
or even create a base abstract class but I don't like it very much.
Similar question: Ef core fluent api set all column types of interface
Is there any better way?