在这种情况下,我如何联系我的实体? [英] How do I relate my entities in this case?

查看:89
本文介绍了在这种情况下,我如何联系我的实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本理念  [ ^ ]



链接的图片显示做什么数据模型,我想通过使用Entity Framework Code First来实现。



另一个例子,有点复杂,假设这些:



- 接口:IPhone,IEmail ,IPerson和IEnterprise

- 实体:实施IEmail的电子邮件,实施IPhone的电话,实施IPerson的学生,用户和供应商以及实施IEnterprise的客户,供应商和办公室;

- 关系角色:

1.来自IPerson和IEnterprise的所有实体可以有零个或多个电话。

2.每个电话只有一个所有者。

3.与电话的每个关系必须存储在相关的链接表中,而不是直接存储在电话的表格上。

4.删除IEnterprise将删除子记录。

5.删除IPerson将删除除了不会删除子记录的用户以外的子记录。

6.查询电话时,我需要获得所有者,无论它是什么类型。 />


所以,我的问题是:如何使用实体解释我的实体Framework 6 Code First / FluentAPI?



提前谢谢。

The basic idea [^]

The linked image shows "what to do" in a data model, and I want to do it by using Entity Framework Code First.

As another example, a bit more complex, suppose these:

- Interfaces: IPhone, IEmail, IPerson and IEnterprise
- Entities: Email that implements IEmail, Phone that implements IPhone, Student, User and Vendor that implements IPerson and Customer, Supplier and Office that implements IEnterprise;
- Relationship Roles:
1. All entities from IPerson and IEnterprise can have zero or many Phones.
2. Each Phone have only one owner.
3. Each relation with Phone must be stored in a related link table and not directly on Phone's table.
4. On delete for IEnterprise will delete child records.
5. On delete for IPerson will delete child records except User that will not delete child records.
6. When querying Phones, I'll need to get the owner, whatever kind it is.

So, my question is: How to relate my entities as explained using Entity Framework 6 Code First/FluentAPI ?

Thank you in advance.

推荐答案

如果我理解你的话...



如果你想在所有者和手机之间创建多对多关系,你需要创建3个表:

1)Supliers_phones

2)Customers_phones

3)Vendors_phone



每个应包含2个外键:

1)Suplier /客户/供应商FK

2)电话FK



但是......

我建议通过创建4个表来规范化数据:

If i understand you well...

You need to create 3 tables, if you want to create many-to-many relationships between owners and phones:
1) Supliers_phones
2) Customers_phones
3) Vendors_phone

Each of it should contain 2 Foreign Keys:
1) Suplier/Customer/Vendor FK
2) Phone FK

But...
I'd suggest to normalize data by creating 4 tables:
OwnerType (OTypeID INT IDENTITY(1,1), ODescription NVARCHAR(155))
Owners (OwnerID INT IDENTITY(1,1), OName NVARCHAR(155), OSurName NVARCHAR(155), ONickName NVARCHAR(155), OBirthDay DATETIME, OTypeID INT (FK - reference to OwnerType table))
Phones (PhoneID INT IDENTITY(1,1), PNumber (?), PCompany (?), PAreaCode(?))
Owners_Phone (OwnerID INT (FK - reference to Owners table), PhoneID INT (FK - reference to Phones table))





注意:这是唯一的建议; )



Note: it's only suggestion ;)


大家好。



特别感谢Maciej Los对这个主题的关注。



我到目前为止找到的解决方案与我在EF4.1中使用的解决方案类似,即:



- 创建一个简单的基本界面,只有Id,我称之为IOwner;

- 创建IEnterprise和IPerson接口,没有属性的注册标识,也就是说,没有密钥;

- 创建一个抽象类(Person)来实现IOwner和IPerson;

- 创建一个抽象类(Enterprise)来实现IOwner和IEnterprise;

- 创建继承自Person的具体类Student,User和Vendor;

- 创建具体类从Enterprise继承的客户,供应商和Office;

- 在实现IOwner和电话和电子邮件表的每个具体类之间创建具体的类连接。



虽然它相当复杂,但结构可以在学生表,用户,供应商,客户,供应商和办公室以及电话和电子邮件表中保持一对多的关系。



但是这个实现将迫使我为从IOwner派生的每个类保留一个专门的存储库,在那里我为电子邮件和手机的CRUD创建了必要的功能。



我认为EF6会有任何可以促进这种实施的消息。如果有人知道在我的问题中引用的其他更专业的创建实体的方式,则是一个尚不存在的强烈迹象。



我采用的解决方案效果很好良好的表现,但我确实得到了一个强烈的印象,成为一个看起来非常学习的解决方案。如果这个解决方案对我以外的任何人都有用,我感到非常高兴。



无论如何,感谢大家的耐心和专注。



加强,如果有人知道如何在我出发的情况下以更专业的方式应用EF6,请提前感谢您的分享。
Hello everyone.

Special thanks to attention given to the subject by Maciej Los.

The solution I found so far was similar will that I used in EF4.1, ie:

- Create a simple basic interface, only with the Id, which I called IOwner;
- Create the IEnterprise and IPerson interfaces without a registration identification of property, that is, without a key;
- Create an abstract class (Person) to implement the IOwner and IPerson;
- Create an abstract class (Enterprise) to implement the IOwner and IEnterprise;
- Create the concrete classes Student, User and Vendor inheriting from Person;
- Create the concrete classes Customer, Supplier and Office inheriting from Enterprise;
- Create concrete classes connection between each of the concrete classes that implement IOwner and Phone and Email tables.

Although it was fairly complex, the structure can maintain a kind of relationship One-to-Many among the Student tables, User, Vendor, Customer, Supplier and Office and the Phone and Email tables.

But this implementation will force me to keep a specialized repository for each class derived from IOwner where I created the necessary functionality to CRUD of emails and phones.

I thought the EF6 would have any news that could facilitate this implementation. If anyone knows other more professional ways of creation of entities quoted in my question, is a strong indication that does not already exist.

The solution I adopted works well and with good performance, but I did get a strong impression of being a solution that looks very "learner". And I'm very pleased if this solution is useful for anyone other than myself.

Anyway, thank you all for your patience and dedicated attention.

And strengthening, if anyone knows how to apply the EF6 in a way that appears to be more professional in case I set out, thank you in advance for sharing.


这篇关于在这种情况下,我如何联系我的实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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