Our team is using the Entity Framework 4.1 ORM. We're not using it in the Code First mode, but we are using the clean POCO generation features from this version.
What we've come across is that we're wanting to create an inherited class based off of an EF POCO, but so far the only way we can see this happening is to have a mapping table in the database. Is there a better way to create this inherited entity? The following code is an example of what we 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 have created:
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're dealing with just classes here, but I'm not sure how the two entities would work with each other.
Any ideas on how to accomplish this while either using Entity Framework or in code would be great. Thanks in advance.
This is called Complex Types in EF4.1 and you have a few options available to you. You can have them in separate tables (called Table Per Type) or you can have them in the same table (called Table per Hierarchy) which means there will be a discriminator column added to the table to tell EF what type it is.
I haven't used it a lot yet, but this appears very very powerful in EF4.1
Check out this multiple part article for a good overview of dealing with Complex Types in EF4.1. It is written against the CTP5, but looks accurate to me.
I think what you want is on this page here.
If you are mapping to an existing DB or want to control your schema manually, this article has another example of dealing with this scenario for an existing database.
I had similar kind of requirement whereby I wanted to use attributes for the generated POCO classes (which used t4 templates). I got the following reply from a really good EF user.
In a nutshell, you got two options, use Code First approach, which gives you full flexibility with your POCO class or else modify the t4 templates to suit your need. If you don't want to use either of the approaches, you can create separate tables in your db.
Unfortunately, we both are in same boat mate and it will take a bit of compromise or more work to get what we want. :)
What you need is to make the base Member class abstract, and your specificMember and similar classes concrete. This would create/require a table for each concrete implementation of the abstract Member class.
Each concrete class can map to a single table. You can have only one DbSet <concreteClass> but you can use this concrete class to hold relational info with many different tables.
In your example, you can re-use FormData, ContactData, ContactAddress in many different Entities derived from Member or something completely different.
But if you want different classes for ContactData or ContactAddress then, you need to pick and choose one of the appropriate Inheritance methods provided by EF4.1
精彩评论