简体   繁体   English

如何使用 EntityFramework Core 在两个模块之间创建关系?

[英]How to create a relation between two modules using EntityFramework Core?

I need to build a relation between two tables without using the primary key property.我需要在不使用主键属性的情况下在两个表之间建立关系。

Here are my entity models这是我的实体模型

public class RealEstateProperty
{
    public int Id { get; set; }
    public string PostalCode { get; set; }
    //... List of all properties

    [ForeignKey(nameof(PostalCode))]
    public virtual RealEstatePropertyPostalCodePriority PostalCodePriority { get; set; }
}

public class RealEstatePropertyPostalCodePriority
{
    public int Id { get; set; }

    // This is a unique property on the database
    public string PostalCode { get; set; }
    public int? Sort { get; set; }

    [ForeignKey(nameof(PostalCode)), InverseProperty(nameof(RealEstateProperty.PostalCodePriority))]
    public ICollection<RealEstateProperty> Properties { get; set; }
}

The above relations throw the following exception上述关系抛出以下异常

InvalidOperationException: The relationship from 'RealEstateProperty.PostalCodePriority' to 'RealEstatePropertyPostalCodePriority.Properties' with foreign key properties {'PostalCode' : string} cannot target the primary key {'Id' : int} because it is not compatible. Configure a principal key or a set of compatible foreign key properties for this relationship.

The end result will need to look something like this最终结果需要看起来像这样

SELECT p.Id, p.PostalCode, z.Sort
FROM RealEstateProperties AS p
LEFT JOIN RealEstatePropertyPostalCodePriorities AS z ON z.PostalCode = p.PostalCode

In order to be used as principal end of a relationship, the non PK property PostalCode of the RealEstatePropertyPostalCodePriority must be configured as alternate key .为了被用作关系的主要端部中,非PK属性PostalCode所述的RealEstatePropertyPostalCodePriority必须配置为备用键 Then the relationship must be configured to use the alternate key rather than the primary key.然后必须将关系配置为使用备用键而不是主键。

Both require fluent API ( HasAlternateKey , HasPrincipalKey ), so remove the ForeignKey and InverveProperty annotations (they are confusing anyway):两者都需要流畅的 API( HasAlternateKeyHasPrincipalKey ),因此删除ForeignKeyInverveProperty注释(无论如何它们都令人困惑):

public class RealEstateProperty
{
    public int Id { get; set; }
    public string PostalCode { get; set; }
    //... List of all properties

    //[ForeignKey(nameof(PostalCode))] <-- remove this...
    public virtual RealEstatePropertyPostalCodePriority PostalCodePriority { get; set; }
}

public class RealEstatePropertyPostalCodePriority
{
    public int Id { get; set; }

    // This is a unique property on the database
    public string PostalCode { get; set; }
    public int? Sort { get; set; }

    //[ForeignKey(nameof(PostalCode)), InverseProperty(nameof(RealEstateProperty.PostalCodePriority))] // <-- ... and this
    public ICollection<RealEstateProperty> Properties { get; set; }
}

and use the following fluent configuration instead:并使用以下流畅的配置:

modelBuilder.Entity<RealEstatePropertyPostalCodePriority>()
    .HasMany(pcp => pcp.Properties)
    .WithOne(p => p.PostalCodePriority)
    .HasForeignKey(p => p.PostalCode)
    .HasPrincipalKey(pcp => pcp.PostalCode);

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

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