EF Core 中按 ID 和类型的关系
Relationship in EF Core by ID and Type
我使用 EF Core(代码优先)我需要通过 ID 和类型在 2 个表之间建立关系
以下是我的类
public class Lead : BaseEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public short Status { get; set; }
public short PhoneType { get; set; }
public string Phone { get; set; }
public short EmailType { get; set; }
public string Email { get; set; }
public string Notes { get; set; }
public List<AddressInformation> AddressInformations { get; set; }
}
public class Opportunity : BaseEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public List<AddressInformation> AddressInformations { get; set; }
}
public class AddressInformationViewModel
{
public int Id { get; set; }
public int SourceID { get; set; } // in this column i need to store ID for Lead or Oppurtunity
public string RelatedTo { get; set; } // in this column i need to store text "Lead" or "Oppurtunity"
public short AddresType { get; set; }
public string Description { get; set; }
}
The AddressInformation class will hold information for Leads or Opportunity based on SourceID and RelatedTo columns
我们如何处理这种关系?
当我进行数据迁移时,EF 将在 Lead Table 中添加新列,列名称为“LeadID”,我不需要这种方法,有没有办法处理这种关系。
我建议为每个关系考虑多对多连接 table,而不是在地址中有效地建立 SourceId + SourceType 关联。 IE。使用 LeadAddress table 和 OpportunityAddress table。使用 EF Core 5 和 EF 6,您可以在没有连接实体的情况下关联它们,只需映射连接 table,或者为早期的 EF Core 创建一个连接实体,或者如果您需要关系中的其他列。
使用特定链接 table 的主要优点是您可以维护 FK 关系。使用 SourceId + SourceType,您不能将 SourceId 用作潜在客户和机会的 FK,但是通过加入 table,LeadId 可以 FK 引导,而 AddressId 可以 FK 处理。这有助于保持查询地址相关详细信息的效率。
要考虑的这种方法的一个好处或局限性是,通过链接 table,地址可以合法地分配给潜在客户和机会,或者在潜在客户/机会之间共享。如果您不想支持在多个实体之间共享地址,则需要实施检查以防止它。这确实意味着将地址视为不同的位置,而不仅仅是特定相关实体的数据容器。例如,123 Smithe St. 始终是 123 Smithe St。更改线索的地址通常意味着将其与具有不同值的新地址对象相关联,而不是编辑 123 Smithe St. 的值(除非他们实际上是要更正地址,即 123 Smith St.)
SourceId + SourceType 可以实现,但据我所知,这必须作为单独的不相关实体处理,并手动与查询连接,即类似于:
var query = context.Leads
.Join(context.Addresses.Where(a => a.RelatedTo == "lead"),
l => l.Id,
a => a.SourceId,
(l,a) => new {Lead = l, Addresses = a})
.Single(x => x.Lead.Id == leadId);
随着查询变得越来越复杂,处理 Join 变得越来越复杂,而且据我所知,您不会从 mapping/navigation 属性中获得像 lead.Addresses 这样有用的东西,您可以在其中 lead.Addresses 或至少 lead.LeadAddresses 使用专用链接 table.
我使用 EF Core(代码优先)我需要通过 ID 和类型在 2 个表之间建立关系 以下是我的类
public class Lead : BaseEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public short Status { get; set; }
public short PhoneType { get; set; }
public string Phone { get; set; }
public short EmailType { get; set; }
public string Email { get; set; }
public string Notes { get; set; }
public List<AddressInformation> AddressInformations { get; set; }
}
public class Opportunity : BaseEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public List<AddressInformation> AddressInformations { get; set; }
}
public class AddressInformationViewModel
{
public int Id { get; set; }
public int SourceID { get; set; } // in this column i need to store ID for Lead or Oppurtunity
public string RelatedTo { get; set; } // in this column i need to store text "Lead" or "Oppurtunity"
public short AddresType { get; set; }
public string Description { get; set; }
}
The AddressInformation class will hold information for Leads or Opportunity based on SourceID and RelatedTo columns
我们如何处理这种关系? 当我进行数据迁移时,EF 将在 Lead Table 中添加新列,列名称为“LeadID”,我不需要这种方法,有没有办法处理这种关系。
我建议为每个关系考虑多对多连接 table,而不是在地址中有效地建立 SourceId + SourceType 关联。 IE。使用 LeadAddress table 和 OpportunityAddress table。使用 EF Core 5 和 EF 6,您可以在没有连接实体的情况下关联它们,只需映射连接 table,或者为早期的 EF Core 创建一个连接实体,或者如果您需要关系中的其他列。
使用特定链接 table 的主要优点是您可以维护 FK 关系。使用 SourceId + SourceType,您不能将 SourceId 用作潜在客户和机会的 FK,但是通过加入 table,LeadId 可以 FK 引导,而 AddressId 可以 FK 处理。这有助于保持查询地址相关详细信息的效率。
要考虑的这种方法的一个好处或局限性是,通过链接 table,地址可以合法地分配给潜在客户和机会,或者在潜在客户/机会之间共享。如果您不想支持在多个实体之间共享地址,则需要实施检查以防止它。这确实意味着将地址视为不同的位置,而不仅仅是特定相关实体的数据容器。例如,123 Smithe St. 始终是 123 Smithe St。更改线索的地址通常意味着将其与具有不同值的新地址对象相关联,而不是编辑 123 Smithe St. 的值(除非他们实际上是要更正地址,即 123 Smith St.)
SourceId + SourceType 可以实现,但据我所知,这必须作为单独的不相关实体处理,并手动与查询连接,即类似于:
var query = context.Leads
.Join(context.Addresses.Where(a => a.RelatedTo == "lead"),
l => l.Id,
a => a.SourceId,
(l,a) => new {Lead = l, Addresses = a})
.Single(x => x.Lead.Id == leadId);
随着查询变得越来越复杂,处理 Join 变得越来越复杂,而且据我所知,您不会从 mapping/navigation 属性中获得像 lead.Addresses 这样有用的东西,您可以在其中 lead.Addresses 或至少 lead.LeadAddresses 使用专用链接 table.