简体   繁体   English

当基础来自EF4.1 DbContext时,如何创建一个继承的类?

[英]How to create an inherited class when the base comes from an EF4.1 DbContext?

Our team is using the Entity Framework 4.1 ORM. 我们的团队正在使用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. 我们没有在Code First模式下使用它,但我们正在使用此版本的干净POCO生成功能。

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. 我们遇到的是我们想要创建一个基于EF POCO的继承类,但到目前为止,我们能够看到这种情况的唯一方法是在数据库中有一个映射表。 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. 在使用Entity Framework或代码中如何实现这一点的任何想法都会很棒。 Thanks in advance. 提前致谢。

This is called Complex Types in EF4.1 and you have a few options available to you. 这在EF4.1中称为复杂类型,您可以使用几个选项。 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. 您可以将它们放在单独的表中(称为“每种类型的表”),也可以将它们放在同一个表中(称为“每个层次结构的表”),这意味着将向表中添加一个鉴别器列以告知EF它的类型。

I haven't used it a lot yet, but this appears very very powerful in EF4.1 我还没有使用它,但是在EF4.1中这看起来非常强大

Check out this multiple part article for a good overview of dealing with Complex Types in EF4.1 . 查看这篇多部分文章,以便更好地了解如何处理EF4.1中的复杂类型 It is written against the CTP5, but looks accurate to me. 它是针对CTP5编写的,但对我来说看起来很准确。

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). 我有类似的要求,我想使用生成的POCO类(使用t4模板)的属性。 I got the following reply from a really good EF user. 我从一个非常好的EF用户那里得到了以下回复

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. 简而言之,您有两个选项,使用Code First方法,它为您提供POCO类的完全灵活性,或者修改t4模板以满足您的需要。 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. 这将为抽象Member类的每个具体实现创建/需要一个表。

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. 您只能有一个DbSet <concreteClass>,但您可以使用此具体类来保存与许多不同表的关系信息。

In your example, you can re-use FormData, ContactData, ContactAddress in many different Entities derived from Member or something completely different. 在您的示例中,您可以在源自Member的许多不同实体或完全不同的实体中重用FormData,ContactData,ContactAddress。

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 但是如果你想要ContactData或ContactAddress的不同类,那么你需要选择一个EF4.1提供的适当的继承方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM