EF4 DB 优先:TPH 方法?
我知道这不应该是微不足道的,但到目前为止还找不到解决方案...
使用 EF4 DB-First 模型,将 LINQ-to-Entities 与 POCO 一起使用,将由 MVC3 应用程序使用。
我有三个实体 Customer
、CustomerAdress
和一个查找 CustomerAddressType
。
Customer CustomerAddress CustomerAddressType
---------- ---------------- -------------------
CustomerId (PK) CustomerAddressId (PK) CustomerAddressTypeId (PK)
LastName CustomerId (FK) Description (Values: Mailing, Billing)
FirstName CustomerAddressTypeId (FK)
MiddleInitial Address
.... City
State
Zip
StartDate
EndDate
正如您所看到的,CustomerAddress
有一个FK CustomerAddressTypeId
,它标识了地址的类型,即邮寄或计费。
我想:
- 有能力执行以下操作:
Customer.CustomerAddress.OfType
来获取客户的邮寄地址集合。 - 具有
CurrentMailingAddress
和CurrentBillingAddress
属性,这将返回具有最高StartDate
的单个实例CustomerAddress.OfType
> 和EndDate
将来。 - 也可以通过
Zip
属性获取Address
并将这些属性重构为复杂类型Address
。
我尝试从 CustomerAddress
创建 2 个继承实体(假设它是 TPH [table-per-hierarchy] 策略): MailingAddress
和 BillingAddress
,CustomerAddressTypeId
是鉴别器。我在模型设计器中执行了此操作,当我尝试添加第二个继承实体时,它告诉我具有这些名称的属性已经存在,并且不允许我重命名它们以匹配第一个实体的属性。
有什么想法如何实现这一点?请帮我简化一下:) 谢谢!!!
I know this should not be trivial, but so far couldn't find the resolution...
Working with an EF4 DB-First model, using LINQ-to-Entities with POCOs which will be consumed by an MVC3 app.
I have three entities Customer
, CustomerAdress
and a lookup CustomerAddressType
.
Customer CustomerAddress CustomerAddressType
---------- ---------------- -------------------
CustomerId (PK) CustomerAddressId (PK) CustomerAddressTypeId (PK)
LastName CustomerId (FK) Description (Values: Mailing, Billing)
FirstName CustomerAddressTypeId (FK)
MiddleInitial Address
.... City
State
Zip
StartDate
EndDate
As you can see CustomerAddress
has a FK CustomerAddressTypeId
, which identifies what type of address this is, i.e. Mailing or Billing.
I would like to:
- Have is ability to do something like this:
Customer.CustomerAddress.OfType<MailingAddress>
to get the collection of mailing addresses for the customer. - Have a
CurrentMailingAddress
andCurrentBillingAddress
properties, that would return the single instanceCustomerAddress.OfType<>
with the highestStartDate
andEndDate
in the future. - Would be also nice to take
Address
thruZip
properties and refactor those propertiess into a Complex TypeAddress
.
I tried creating 2 inherited entities off of CustomerAddress
(assuming it is TPH [table-per-hierarchy] strategy):MailingAddress
and BillingAddress
, CustomerAddressTypeId
being the discriminator. I did this in the model designer, and as soon as I tried adding a second inherited entity, it told me that the properties with those names already existed, and wouldn't let me rename them to match the properties of the first entity.
Any ideas how to accomplish this? Please dumb it down for me :)
Thanks!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这并不是一件小事。 TPH 是可能的,但您必须将所有属性放置到基本
CustomerAddress
中,并派生两个子实体,它们不会保存任何属性,因为所有属性都是共享的(= 必须位于父级中)。您将使用CustomerAddressTypeId
作为鉴别器,因此您将无法将此字段映射为实体中的属性。我也不确定你是否可以在鉴别器和关联映射中都拥有该字段(这对我来说实际上是很好的作业)。否则,您将无法映射CustomerAddress
和CustomerAddressType
之间的关联。CurrentMailingAddress
和CurrentBillingAddress
都是计算属性,它们不是映射的一部分。您可以在Customer
实体的部分中实现他们的逻辑。我不明白
Zip
和复杂类型的最后一点。It is not such trivial. TPH will be possible but you must place all properties to the base
CustomerAddress
and derive two sub entities which will not hold any property because all properties are shared (= must be in the parent). You will useCustomerAddressTypeId
as discriminator and because of that you will not be able to map this field as property in the entity. I'm also not sure if you can have the field both in discriminator and association mapping (that is actually nice homework for me). If not you will not be able to map association betweenCustomerAddress
andCustomerAddressType
.Both
CurrentMailingAddress
andCurrentBillingAddress
are computed properties and they are not part of mapping. It is up to you to implement their logic in your partial part ofCustomer
entity.I don't understand the last point with
Zip
and complex type.