【问题标题】:Conditional navigation properties in Entity Framework CoreEntity Framework Core 中的条件导航属性
【发布时间】:2019-02-20 03:03:16
【问题描述】:

我正在尝试在 EF Core 中创建一个导航属性,该属性将根据两个属性的值有条件地设置其引用。我不确定这是否可能。

让我举个例子:假设我有一个实体的层次结构,例如CountryStateCountyCity。我还有一个名为Law 的实体,它可以由任何分层实体“拥有”。

所以,我创建了这个枚举:

public enum OwnerType
{
    Country,
    State,
    County,
    City
}

...和Law 类:

public class Law
{
    public int Id { get; set; }
    public OwnerType OwnerType { get; set; }
    public int OwnerId { get; set; }
}

现在我想设置Law 类,使其具有一个导航属性,该属性将OwnerId 链接到基于OwnerType 值的相应实体的主键。

我考虑将其添加到 Law 类中:

public virtual object Owner { get; set; }

或者创建每个分层实体都将实现的IOwner 接口,然后我会添加它:

public virtual IOwner Owner { get; set; }

但是我不知道如何使用EntityTypeBuilder 设置EntityTypeConfiguration。这显然行不通:

builder.HasOne(x => x.Owner).WithMany(x => x.Laws).HasForeignKey(x => x.OwnerId);

我真的不知道如何完成我在这里尝试做的事情。有什么想法吗?

【问题讨论】:

  • 我建议你画个类图你就明白了。

标签: c# .net-core entity-framework-core navigation-properties


【解决方案1】:

我可以看到你有 4 个不同的关系,你想用一个外键处理它们,这在概念上是个坏主意。如果您有 4 个关系 - 您需要有 4 个 FK。

在纯 OOP 中,您可以使用 IOwner 接口,但实体框架需要明确的信息来分别映射您的关系,我相信这是最好的方法。只需添加 4 个不同的可空 FK 并使用 OwnerType 值验证 Law 状态。

public class Law {
    public int Id { get; set; }
    public OwnerType OwnerType { get; set; }

    [ForeignKey(nameof(Country)]
    public int? CountryId { get; set; }
    public Country Country { get; set; }

    [ForeignKey(nameof(State)]
    public int? StateId { get; set; }
    public State State { get; set; }

    [ForeignKey(nameof(County)]
    public int? CountyId { get; set; }
    public County County { get; set; }

    [ForeignKey(nameof(City)]
    public int? CityId { get; set; }
    public City City { get; set; }

    private void Validate() {
        switch (OwnerType)
        {
            case OwnerType.Coutnry:
                if(CountryId == null)
                    throw new LawValidationException("Country is requried");
            break;
            case OwnerType.State:
                if(StateId == null)
                    throw new LawValidationException("State is requried");
            break;
            case OwnerType.County:
                if(CountyId == null)
                    throw new LawValidationException("County is requried");
            break;
            case OwnerType.City:
                if(CityId == null)
                    throw new LawValidationException("City is requried");
            break;
            default:
                    throw new LawValidationException("Invalid law owner type");
        }
    }
}

这种方法可以解决您的问题,完全符合实体框架的能力,并且可以轻松集成到包括单元测试在内的外部逻辑中。

【讨论】:

  • 我担心这将是我唯一的选择。我试图在一个 FK 关系中这样做有两个原因:1)。以避免必须将所有这些代码添加到可以由这些OwnerTypes 之一“拥有”的每个实体中,以及 2)。如果我将来添加一个新的OwnerType,我就不必返回并手动更新每个实体来获得一个新的可为空的 FK。如果没有办法,我很好。
  • 如果您的实体没有从其他基类继承,但您可以添加 OwnedEntity 类(例如),将所有这些 FK 导出到那里,然后从它继承 Law 和其他类。然后您只需要添加另一个迁移,Entity Framework 将检测模型更改并相应地更新数据库架构。在这种情况下,您仍然需要在每次出现新的OwnerType 时更新代码,但只能在一个地方进行。
猜你喜欢
  • 2023-03-17
  • 2017-01-01
  • 2021-05-17
  • 1970-01-01
  • 1970-01-01
  • 2020-01-21
  • 1970-01-01
  • 1970-01-01
  • 2020-06-27
相关资源
最近更新 更多