有时从这样的基类派生我的实体是有用的:
public abstract class DestructableBase : IDestructable
{
/// <summary>
/// If true, this object should be deleted from the database.
/// </summary>
[NotMapped]
public bool _destroy { get; set; }
}
这允许Web客户端将数据标记为在将数据发布回服务器时需要删除。显然我不希望在数据库中记录这样的属性,所以我使用[NotMapped]属性。
我已经开始越来越多地使用流畅的API来完成我的配置,并希望停止使用数据注释。有没有办法使用流畅的API来执行此操作,而无需单独在每个实体上设置Ignore()?或者有更好的方法吗?
答案 0 :(得分:8)
您可以尝试将此类用作实体配置的基类:
public class DestructableBaseConfiguration<TEntity> : EntityTypeConfiguration<TEntity>
where TEntity : DestructableBase
{
public DestructableEntityConfiguration()
{
Ignore(e => e._destroy);
}
}
现在,从DestructableBase
派生的每个其他实体都需要从DestructableBaseConfiguration
派生的实体配置类。您需要将配置注册到modelBuilder
中的OnModelCreating
。