EF4 DB 优先:TPH 方法?

发布于 2024-11-02 11:06:23 字数 1647 浏览 5 评论 0原文

我知道这不应该是微不足道的,但到目前为止还找不到解决方案...

使用 EF4 DB-First 模型,将 LINQ-to-Entities 与 POCO 一起使用,将由 MVC3 应用程序使用。

我有三个实体 CustomerCustomerAdress 和一个查找 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 来获取客户的邮寄地址集合。
  • 具有 CurrentMailingAddressCurrentBillingAddress 属性,这将返回具有最高 StartDate 的单个实例 CustomerAddress.OfType > 和 EndDate 将来。
  • 也可以通过 Zip 属性获取 Address 并将这些属性重构为复杂类型 Address

我尝试从 CustomerAddress 创建 2 个继承实体(假设它是 TPH [table-per-hierarchy] 策略): MailingAddressBillingAddressCustomerAddressTypeId 是鉴别器。我在模型设计器中执行了此操作,当我尝试添加第二个继承实体时,它告诉我具有这些名称的属性已经存在,并且不允许我重命名它们以匹配第一个实体的属性。

有什么想法如何实现这一点?请帮我简化一下:) 谢谢!!!

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 and CurrentBillingAddress properties, that would return the single instance CustomerAddress.OfType<> with the highest StartDate and EndDate in the future.
  • Would be also nice to take Address thru Zip properties and refactor those propertiess into a Complex Type Address.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风为裳 2024-11-09 11:06:23

这并不是一件小事。 TPH 是可能的,但您必须将所有属性放置到基本 CustomerAddress 中,并派生两个子实体,它们不会保存任何属性,因为所有属性都是共享的(= 必须位于父级中)。您将使用 CustomerAddressTypeId 作为鉴别器,因此您将无法将此字段映射为实体中的属性。我也不确定你是否可以在鉴别器和关联映射中都拥有该字段(这对我来说实际上是很好的作业)。否则,您将无法映射 CustomerAddressCustomerAddressType 之间的关联。

CurrentMailingAddressCurrentBillingAddress 都是计算属性,它们不是映射的一部分。您可以在 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 use CustomerAddressTypeId 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 between CustomerAddress and CustomerAddressType.

Both CurrentMailingAddress and CurrentBillingAddress are computed properties and they are not part of mapping. It is up to you to implement their logic in your partial part of Customer entity.

I don't understand the last point with Zip and complex type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文