[英]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(
HasAlternateKey
、 HasPrincipalKey
),因此删除ForeignKey
和InverveProperty
注释(无论如何它们都令人困惑):
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.