c# - Entity Framework Core 中的条件导航属性

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

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

让我举个例子:假设我有一个层次结构的实体,例如 CountryStateCounty城市。我还有一个名为 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 接口(interface),每个分层实体都将实现,然后我会添加这个:

public virtual IOwner Owner { get; set; }

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

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

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

最佳答案

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

在纯 OOP 中,您可以使用 IOwner 接口(interface),但 Entity Framework 需要显式信息来分别映射您的关系,我相信这是最好的方法。只需添加 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");
        }
    }
}

这种方法解决了您的问题,非常适合 Entity Framework 的能力,并且可以轻松集成到包括单元测试在内的外部逻辑中。

关于c# - Entity Framework Core 中的条件导航属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54778176/

相关文章:

c# - 获取c#函数值并在html表中显示

c# - 在 MigraDoc TextFrame 中测量字符串

c# - Xunit - 以列表或对象为参数的单元测试

c# - 在 OxyPlot 线系列中添加间隙

c# - 如何使用 MonoMac/Xamarin 打开或创建新窗口?

.net - 找不到与命令 "dotnet-tool"匹配的可执行文件

c# - 尝试诊断 Azure ASP.NET Core WebAPI 应用程序中的内存泄漏

entity-framework - EF 7 身份插入问题

sqlite - 无法加载文件或程序集 SQLitePCLRaw.core

c# - 为什么我调用 ASPNET Core 2.1 Database Migrate 的实现实际上没有应用迁移?