实体框架6 - 具有附加信息的多对多表 [英] Entity Framework 6 - Many-to-Many Table with Additional Information

查看:92
本文介绍了实体框架6 - 具有附加信息的多对多表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来我不允许张贴图像,所以让我来描述这个图像。它是一个SQL表,显示了4个表之间的关系。表格是:

Seems I am not allowed to post images, so let me describe the image. It is a SQL table diagram showing the relationships between 4 tables. The Tables are:


  1. 人民币

    • ID

    • FirstName

    • 姓氏


  • ID

  • 号码


  • ID

  • 名称

  • 描述


  • PersonId

  • PhoneNumberTypeId

  • PhoneNumberId

两个主表是People和PhoneNumbers。还有一个PhoneNumberTypes描述了PhoneNumber(Home,Work等)的类型。 PeoplePhoneNumbers表用作People和PhoneNumbers之间的多对多关系表,

The two main tables are People and PhoneNumbers. There is also a PhoneNumberTypes that describes the type of PhoneNumber (Home, Work, etc).

但是,它也连接到PhoneNumberTypes来描述关系。

The PeoplePhoneNumbers table serves as a Many-To-Many relationship table between People and PhoneNumbers. However it also connects to PhoneNumberTypes to describe the relationship.

我一直试图找出如何处理这个实体框架,因为EF不允许你添加额外的信息协会(多对多)表。

I have been trying to figure out how to handle this Entity Framework because EF does not allow you to add additional information to the Association(Many-To-Many) Table.

除了PhoneNumberType信息,我还发现他们是在关联表中需要记录的附加数据,如开始日期,结束日期等。

Besides the PhoneNumberType info, I also find that their are additional data pieces I need to record in the Association Table like "Start Date", "End Date", etc.

到目前为止我唯一想到的解决方案是在EF中创建一个实体,它将PhoneNumberTypes中的字段, PhoneNumbers和PeoplePhoneNumbers成为一个实体。然后使用SQL存储过程进行CRUD操作。

The only solution I have come up with so far is to create an entity in EF that combines the fields in PhoneNumberTypes, PhoneNumbers and PeoplePhoneNumbers into a single entity. Then use SQL stored procedures for CRUD operations against it.

我更喜欢一个更加以EF为中心的解决方案。有人知道一个吗?

I would prefer a more EF centric solution. Does anyone know of one?

推荐答案

PhoneNumberTypes不应该链接到PeoplePhoneNumbers。让PeoplePhoneNumbers成为它只是一个连接表。 PhoneNumberTypes应链接到PhoneNumbers。 EF应该能够按照惯例创建此设置。描述电话号码的附加信息也应该与PhoneNumbers关联。该设计遵循更多的域驱动设计(DDD)原则,并且还可以生成更好的数据库设计。

PhoneNumberTypes should not be linked to PeoplePhoneNumbers. Let PeoplePhoneNumbers be what it is and only a junction table. PhoneNumberTypes should be linked to PhoneNumbers. EF should be able to create this setup by convention. Additional information describing phone number should also be linked off of PhoneNumbers. This design adheres to more Domain Driven Design (DDD) principles and also generates a better database design as well.

这篇关于实体框架6 - 具有附加信息的多对多表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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