How to create an inherited class when the database comes from Eb4.1 DbContext?

Our team uses Entity Framework 4.1 ORM. We do not use it in Code First mode, but we use pure POCO generation functions from this version.

We are faced with the fact that we want to create an inherited class based on EF POCO, but for now the only way to see this is to have a mapping table in the database. Is there a better way to create this inherited object? The following code is an example of what I'm talking about.

This class is generated:

public partial class Member { public Member() { this.ContactAddresses = new HashSet<ContactAddress>(); this.ContactEmails = new HashSet<ContactEmail>(); this.FormDatas = new HashSet<FormData>(); this.ContactPhones = new HashSet<ContactPhone>(); } public int ID { get; set; } public string FirstName { get; set; } public string MiddleName { get; set; } public string LastName { get; set; } public string Alias { get; set; } public string Title { get; set; } public string Company { get; set; } public string Prefix { get; set; } public string Suffix { get; set; } public System.DateTime CreatedDate { get; set; } public int CreatedBy { get; set; } public Nullable<System.DateTime> ModifiedDate { get; set; } public Nullable<int> ModifiedBy { get; set; } public Nullable<System.Guid> AspNetUserID { get; set; } public virtual aspnet_Users aspnet_Users { get; set; } public virtual ICollection<ContactAddress> ContactAddresses { get; set; } public virtual ICollection<ContactEmail> ContactEmails { get; set; } public virtual ICollection<FormData> FormDatas { get; set; } public virtual ICollection<ContactPhone> ContactPhones { get; set; } } 

This is an example of what we would like to create:

 public class SpecificMember : Member { public SpecificMember() { this.Assignments = new HashSet<Assignment>(); this.Expertises = new HashSet<Expertise>(); this.Experiences = new HashSet<Experience>(); }this.VendorInformations = new HashSet<VendorInformation>(); public virtual ICollection<Assignment> Assignments { get; set; } public virtual ICollection<Expertise> Expertises { get; set; } public virtual ICollection<Experience> Experiences { get; set; } public virtual ICollection<VendorInformation> VendorInformations { get; set; } } 

I would really like to stay away from placing another table in the database. My idea is that we only deal with classes, but I'm not sure how these two objects will work with each other.

Any ideas on how to accomplish this when using the Entity Framework or in code would be great. Thanks in advance.

+4
source share
3 answers

This is called complex types in EF4.1, and you have several options available to you. You can use them in separate tables (for example, Table Per Type), or you can have them in one table (called a table per hierarchy), which means that a discriminator column will be added to the table to tell EF what type it is.

I have not used it yet, but in EF4.1 it looks very strong.

Check out this article with several parts to get a good overview of working with Complex Types in EF4.1 . It is written against CTP5, but it looks exactly to me.

I think you want this page here .

If you are mapping an existing database or want to manually manage your schema, this article has another example of how to consider this scenario for an existing database.

+3
source

I had a similar requirement when I wanted to use attributes for the generated POCO classes (which used t4 templates). I received the following response from a really good EF user.

In a nutshell, you have two options: use the Code First approach, which gives you full flexibility with your POCO class or modifies t4 templates to suit your needs. If you do not want to use any of the approaches, you can create separate tables in your db.

Unfortunately, we are both in the same friendly match, and in order to get what we want, it will take a little compromise or more work. :)

+2
source

What you need to do is make the base class of the participants abstract, and your concrete class and similar classes concrete. This would create / require a table for each specific implementation of the Member abstract class.

Each concrete class can display one table. You can have only one DbSet <concreteClass> but you can use this particular class to store relational information with many different tables.

In your example, you can reuse FormData, ContactData, ContactAddress in many different entities received from a member or something completely different.

But if you need different classes for ContactData or ContactAddress, then you need to choose one of the appropriate inheritance methods provided by EF4.1

+1
source

All Articles