继承EF代码优先 [英] Inheritance EF Code-First

查看:128
本文介绍了继承EF代码优先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本对象,我不想在DB中作为一个实体映射,我只希望将属性添加到映射到DB中的对象:



未映射的对象(不知道是否重要,但是baseobject在另一个程序集中):

  public class BaseObject 
{
public virtual string Prop1 {get;组; }
public virtual string Prop2 {get;组;
}

映射对象:

  public class ChildObject:BaseObject 
{
public virtual string Prop3 {get;组; }
public virtual string Prop4 {get;组; }
public virtual string Prop5 {get;组;
}

在DbContext中注册的内容

  public DbSet< ChildObject> ChildObjects {get;组; 

我想在Db中看到什么

 表:ChildObject 
col:Prop1(来自BaseObject)
col:Prop2(来自BaseObject)
col:Prop3
col :Prop4
col:Prop5

要恢复,我想做的是要有一个Db表中有一个小孩和基本属性。



这是我目前正在收到的错误:


类型'namespace.ChildObject'未映射。通过使用Ignore方法或
NotMappedAttribute数据注释,检查类型
是否未被明确排除。验证类型是否定义为
作为类,不是原始的,嵌套的或通用的,并且不会从EntityObject继承


我一直在挖掘,但找不到如何做到这一点。



任何想法?



编辑:



@Kyle Trauberman是对的,但是,由于某种原因似乎继承了一个基类在不同的汇编。我只是这样做。

  class BaseObjectClone:BaseObject {} / * BaseObject在另一个程序集中* / 

public class ChildObject:BaseObjectClone {
public virtual string Prop3 {get;组; }
public virtual string Prop4 {get;组; }
public virtual string Prop5 {get;组; }
}


解决方案

Morteza Manavi有一个博客详细说明如何执行此操作:



http:// weblogs .asp.net / manavi /存档/ 2011/01/03 /继承映射的策略与 - 实体框架代码先CTP5部分-3-表每混凝土型-TPC和 - 选择-strategy-guidelines.aspx



基本上,您需要覆盖 OnModelCreating code> DbContext ,并为每个子表调用 MapInheritedProperties()

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< BankAccount>()。Map(m =>
{
m.MapInheritedProperties();
m.ToTable(Ba nkAccounts);
});

modelBuilder.Entity< CreditCard>()。Map(m =>
{
m.MapInheritedProperties();
m.ToTable(CreditCards ;
});
}


I have a base object that I dont want to be mapped in DB as an entity, I only want the properties to be added to the object that is mapped in the DB :

Not mapped object (dont know if it matters but baseobject is in another assembly):

public class BaseObject
{
    public virtual string Prop1 { get; set; }
    public virtual string Prop2 { get; set; }
}

Mapped object:

public class ChildObject : BaseObject
{
    public virtual string Prop3 { get; set; }
    public virtual string Prop4 { get; set; }
    public virtual string Prop5 { get; set; }
}

What is registered in DbContext

  public DbSet<ChildObject> ChildObjects { get; set; }

What i'd like to see in Db

table:ChildObject 
    col:Prop1 (from BaseObject)
    col:Prop2 (from BaseObject)
    col:Prop3 
    col:Prop4 
    col:Prop5

To resume, what I want to do, is to have one table in the Db that has the child and the base properties.

This is the error i'm currently getting:

The type 'namespace.ChildObject' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject.

I've been digging around, but can't find how to do this.

Any ideas?

EDIT:

@Kyle Trauberman is right, however, for some reason there seems to be a problem with inheriting from a base class in different assembly. I simply did this.

class BaseObjectClone : BaseObject { } /* BaseObject being in another assembly */

public class ChildObject : BaseObjectClone {
    public virtual string Prop3 { get; set; }
    public virtual string Prop4 { get; set; }
    public virtual string Prop5 { get; set; }
}

解决方案

Morteza Manavi has a blog post detailing how to do this:

http://weblogs.asp.net/manavi/archive/2011/01/03/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines.aspx

Basically, you'll need to override OnModelCreating in your DbContext and call MapInheritedProperties() for each of the child tables.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<BankAccount>().Map(m =>
    {
        m.MapInheritedProperties();
        m.ToTable("BankAccounts");
    });

    modelBuilder.Entity<CreditCard>().Map(m =>
    {
        m.MapInheritedProperties();
        m.ToTable("CreditCards");
    });            
}

这篇关于继承EF代码优先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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