【发布时间】:2026-02-07 06:50:01
【问题描述】:
我的模型中有以下模型:
Patient
Vendor
Organization
每个实体都需要地址。
地址基本上如下所示
Address
AddressTypeId // with Navigation Property/Association to AddressType
EntityKey // indicates the PK Id of the entity this address is for
AddressType
EntityId // indicates the entity type this address type corresponds to (Patient or Vendor)
// This should be on the AddressType, not the Address, since we need a way of knowing what kind of AddressTypes are available to create for new addresses for Patients, Vendors, and Organizations
//...that is Patients support AddressType X, Vendors support AddressType Y, etc.
我想在 Address 上的 EntityKey 属性上为 Patient、Vendor 和 Organization 创建一个关联 - 每个都有一个过滤器约束,即 Address 的 AddressType.EntityId 是该实体的匹配 EntityId(1 代表患者,2 代表供应商, 3 代表地址)。
这样做的最佳方法是什么?市场上的大多数 ORM 都支持这种情况......而且它肯定是一种非常常见的情况。
注意:我不想创建 PatientAddress/PatientAddressType、VendorAddress/VendorAddressType 和 OrganizationAddress/OrganizationAddress 类型派生实体。它使模型严重混乱,使其基本上难以理解。
现在我正在通过在我的 LINQ 查询中进行显式连接来解决这个问题:
const int patientTypeEntityId = 1;
var query = from p in repository.Patients
let addresses = repository.Addresses.Where(a =>
a.EntityKey == p.Id & a.AddressType.EntityId == patientTypeEntityId)
select new { Patient = p, Addresses = a }
但我不想继续这样做。
【问题讨论】:
标签: entity-framework entity-framework-4 entity-framework-4.1