Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted

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:
C#
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 ;)
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900