EF核心对同一实体的多个引用 [英] EF Core Multiple References to Same Entity

查看:26
本文介绍了EF核心对同一实体的多个引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.NET Core 2.1.12设置中有一个Organization实体和Address实体.一个组织有0或1个PrimaryAddress以及0或1个BillingAddress,它们的类型均为Address.我正在尝试多种尝试的方法.

I have an Organization entity and Address entity in a .NET Core 2.1.12 setup. An organization has 0 or 1 PrimaryAddress and 0 or 1 BillingAddress, both of type Address. I'm struggling with multiple different approaches I have tried.

这两个链接都看起来很有希望,但是都用于实体的ICollection<>(好像有多个可能的帐单邮寄地址,而不是0或1),而不是单个实体,而且我似乎无法过去那.我也尝试过[InverseProperty]属性,但是没有运气.

These two links both looked promising, but both are for ICollection<> of an entity (as if there were multiple possible billing addresses rather than 0 or 1), not a single entity, and I can't seem to get past that. I have also tried the [InverseProperty] attribute with no luck.

https://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in-code-first.aspx 实体框架代码优先-来自同一表的两个外键

一个旁注认为我对此也是因为Address的复数是Address es ("ES"而不仅仅是一个"S"),并且不确定是否与任何已构建的内容混淆基于命名约定的功能.

One side note thought I had about this was also that the plural of Address is Addresses ("ES" not just an "S") and not sure if that messes with any kind of built in functionality based on naming conventions.

public class Organization : Trackable
{
    [Required]
    public int OrganizationId { get; set; }

    [Required]
    [MaxLength(100)]
    public string OrganizationName { get; set; }

    // other scalar properties

    public int PrimaryAddressId { get; set; }
    public virtual Address PrimaryAddress { get; set; }

    public int BillingAddressId { get; set; }
    public virtual Address BillingAddress { get; set; }
}

public class Address : Trackable
{
    [Required]
    public int AddressId { get; set; }

    [Required]
    [MaxLength(100)]
    public string Street1 { get; set; }

    // other scalar properties
}

任何人都可以对设置此功能的最佳方法提供一些见识吗?

Can anyone provide some insight into the best way to set this up?

谢谢!

推荐答案

这似乎是您一直在依靠EF的约定来解析模型的架构.这适用于基本的东西,但魔力并不涵盖所有内容.要具有两个对同一实体类型的引用,您需要显式配置FK:

This looks like you have been relying on convention for EF to resolve the schema for your model. This works for basic stuff, but the magic doesn't cover everything. To have two references to the same entity type you need to explicitly configure the FK:

[ForeignKey("PrimaryAddress")]
public int PrimaryAddressId { get; set; }
public virtual Address PrimaryAddress { get; set; }

[ForeignKey("BillingAddress")]
public int BillingAddressId { get; set; }
public virtual Address BillingAddress { get; set; }

将地址表设置为地址"(我相信EF可以自动执行此操作,但为了安全,明确地进行配置)

To make the Address table "Addresses" (I believe EF may do this automatically, but to be safe, explicit configuration)

[Table("Addresses")]
public class Address
{
   // ...
}

值得您熟悉实体的显式配置,例如使用属性和/或 IEntityTypeConfiguration< T> (EF核心)或OnModelCreating DbContext事件.我倾向于只默认使用显式配置,以避免在EF出现问题并感到不满意时避免出现意外情况.

It's worth familiarizing yourself with explicit configuration for entities and such using the attributes and/or the IEntityTypeConfiguration<T> (EF Core) or OnModelCreating DbContext event. I tend to just default to explicit configuration to avoid surprises when EF goes and gets unhappy with something.

将EF连接到具有不友好命名约定(例如ALL_CAPS_UNDERSCORE_FTW)的现有数据库时,显式配置非常有用.这使您可以使代码更具可读性,而不必担心属性名称的怪异.:)

Explicit config is very useful when wiring up EF to an existing database that has unfriendly naming conventions like ALL_CAPS_UNDERSCORE_FTW. This allows you to make your code a lot more readable without those monstrosities for property names. :)

这篇关于EF核心对同一实体的多个引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆