9

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; }
}
Pierluc SS
  • 3,138
  • 7
  • 31
  • 44
  • 2
    So, you don't want the properties defined in the base class mapped to the database? Or do you want them mapped? What are you seeing now, vs what you want to see? – Kyle Trauberman Mar 27 '12 at 18:01
  • sorry it might be unclear. I meant i dont want the baseobject to be mapped as a different entity but i want to the properties to show in the child object. – Pierluc SS Mar 27 '12 at 18:04
  • I was able to solve my similar case with my answer [here](https://stackoverflow.com/a/45762308/1718832) – Vladislav Kostenko Aug 18 '17 at 18:01

1 Answers1

10

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");
    });            
}
Kyle Trauberman
  • 25,414
  • 13
  • 85
  • 121
  • Morteza had a whole series of posts about inheritance that you should check out. Here's part 1: http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx – Kyle Trauberman Mar 27 '12 at 18:10
  • I already went through this article, but maybe I'm missing something because it's still throwing an error on the 'ChildObject' – Pierluc SS Mar 27 '12 at 18:11
  • i'm getting the following error. But when I removed the inheritance, its all fine. 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. – Pierluc SS Mar 27 '12 at 18:12
  • 2
    Thanks for writing an answer with a link in it [that I can actually upvote.](http://meta.stackexchange.com/questions/94022) – Robert Harvey Mar 27 '12 at 19:03
  • @Burnzy I just tried to recreate this based on the code you posted, but the default generated tables included properties from both the base class and the child class. There was no separate table generated for the base class. Can you post your complete dbcontext and model classes, instead of the watered-down example that you posted? – Kyle Trauberman Mar 27 '12 at 19:34
  • hmm thats pretty weird, I had it right but it would take inheritance from another assembly for some reason. I will edit my post for solution. – Pierluc SS Apr 05 '12 at 12:30