RubyHaus RubyHaus - 8 months ago 59
C# Question

Entity Framework 5.0 and Inheritance

I have a couple of classes setup like so:

Member (abstract)

  • Property1

  • Property2 (etc.)

OrganizationMember : Member

  • Property3

  • Property4 (etc.)

PrivateMember : Member

  • Property5

  • Property6 (etc.)

I then have three database tables structured the same way - data common to both types of member entities is stored in the Member table, data specific to OrganizationMember is stored in a corresponding table and data specific to PrivateMember is stored in a corresponding table. The Id for the Member table is the same as the Id for the other two tables since it's a 1-1 relationship - just a classification where each type has stuff specific to it. It probably doesn't matter here, but nothing can be a Member directly - they have to be one of the two types.

When I setup my mapping (using Fluid - everything with EF is handcoded, no designers and no EDMX files), if I tell OrganizationMember to ignore properties inherited from Member, I get the "You cannot use Ignore method on the property '[PropertyName]' on type '[TypeName]' because this type inherits from the type '[BaseType]' where this property is mapped. To exclude this property from your model, use NotMappedAttribute or Ignore method on the base type." error. But when I tell it not to ignore those properties inherited from Member it throws an error ("Problem in mapping fragments starting at [...] [TypeName] are being mapped to the same rows in table Member. Mapping conditions can be used to distinguish the rows that these types are mapped to."). The really frustrating part here is the properties erroring out aren't supposed to be mapped at all for either entity - they are part of my overall framework design, but have no database relevancy at all and don't exist there.

I would've assumed this was a pretty standard thing to setup, but EF is really not playing nice. Any suggestions?

Answer Source

This article describes the mapping of inheritance types. It lists different ways to handle mapping to inheritance depending on what you want to achieve.