简体   繁体   English

EF Core 中的零或一关系

[英]Zero-or-one relationship in EF Core

I have a somewhat special scenario of a Zero-or-one relationship in EF Core which cannot be easily modeled by the default (as described, eg,in EF Core One to One or Zero Relationship ).我在 EF Core 中有一个零或一关系的特殊场景,默认情况下无法轻松建模(如EF Core 一对一或零关系中所述)。

I've got multiple entities with a 0-1 relationship to a "Match" entity.我有多个实体与“匹配”实体具有 0-1 关系。 Multiple instances can reference the same "Match" entity (which is why I can't put the foreign key into the "Match" table, which seems to be the recommended way of modeling a 0-1 relationship).多个实例可以引用同一个“匹配”实体(这就是为什么我不能将外键放入“匹配”表的原因,这似乎是建模 0-1 关系的推荐方式)。

How to define the relationship from one of the entities to 0-1 "Match" entities?如何定义从实体之一到 0-1“匹配”实体的关系? Do I have to create a Match().HasMany(someKindOfBaseClassOfEntities) instead?我必须创建一个 Match().HasMany(someKindOfBaseClassOfEntities) 吗? Is there a better way?有没有更好的办法?

You can configure this by using HasOne , which allows a 1:0..1 relationship.您可以使用HasOne进行配置,它允许 1:0..1 的关系。 Just showing one way of doing this without repeating too much mapping code:仅展示一种无需重复太多映射代码的方法:

Classes:课程:

class Match
{
    public int ID { get; set; }
    public string Name { get; set; }
}

abstract class HasMatchBase
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int? MatchID { get; set; }
    public Match Match { get; set; }
}

class A : HasMatchBase
{
    public string Code { get; set; } // Just an example
}

class B : HasMatchBase { }

Mapping configuration:映射配置:

abstract class HasMatchBaseConfig<T> : IEntityTypeConfiguration<T>
    where T : HasMatchBase
{
    public void Configure(EntityTypeBuilder<T> builder)
    {
        builder.Property(b => b.Name).HasMaxLength(200);
        builder.HasOne(b => b.Match).WithOne().HasForeignKey<T>(b => b.MatchID);

        ConfigureMatchOwner(builder);
    }
    
    public abstract void ConfigureMatchOwner(EntityTypeBuilder<T> builder);
}

class AConfig : HasMatchBaseConfig<A> 
{ 
    public override void ConfigureMatchOwner(EntityTypeBuilder<A> builder)
    {
        builder.Property(a => a.Code).HasColumnType("CHAR").HasMaxLength(3);
        ... // Further A-specific mapping
    }
}

class BConfig : HasMatchBaseConfig<B>
{
    public override void ConfigureMatchOwner(EntityTypeBuilder<B> builder)
    {
        ... // B-specific mapping. 
    }
}

The type of HasMatchBase.MatchID determines whether the association is required or optional. HasMatchBase.MatchID的类型决定了关联是必需的还是可选的。 In this case it's a nullable int , turning the association into Match being optional.在这种情况下,它是一个可为空的int ,将关联转换为Match是可选的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM