gpt4 book ai didi

entity-framework - 流畅的 API 可以在从基类继承的所有实体上设置 NotMapped 吗?

转载 作者:行者123 更新时间:2023-12-04 07:22:52 26 4
gpt4 key购买 nike

有时从这样的基类派生我的实体很有用:

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] 属性。

我已经开始越来越多地使用 fluent API 来进行我的配置,并希望停止使用数据注释。有没有办法使用 fluent API 来做到这一点,而不必在每个实体上单独设置 Ignore()?或者有更好的方法吗?

最佳答案

您可以尝试将此类用作实体配置的基类:

public class DestructableBaseConfiguration<TEntity> : EntityTypeConfiguration<TEntity>
where TEntity : DestructableBase
{
public DestructableEntityConfiguration()
{
Ignore(e => e._destroy);
}
}

现在每个其他实体都派生自 DestructableBase需要从 DestructableBaseConfiguration 派生的实体配置类.您将把您的配置注册到 modelBuilderOnModelCreating .

关于entity-framework - 流畅的 API 可以在从基类继承的所有实体上设置 NotMapped 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8525409/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com