Entity Framework – Inheritance/Association – including Troubleshooting and comment on verbose SQL
30-Jul-1212 Leave a comment
See this video on Model Table Per Type
http://msdn.microsoft.com/en-us/data/cc765425.aspx
- Update Model from Database – Add Table
- Right-click Entity Add Inheritance
- Error 11009: Property ‘<>’ is not mapped
- Right-click Entity > Table Mapping > re map missing
- Running transformation: A member named ID cannot be defined in class DerivedClass. It is defined in ancestor class ParentClass.
- Delete it from DerivedClass
Error 3027: No mapping specified for the following EntitySet/AssociationSet – TableATableB
Error 11008: Association TableATableB is not mapped
EF is not expecting both a Foreign Key relationship and an association, see note at top of: http://msdn.microsoft.com/en-us/library/bb738488.aspx
Delete the association
Existing queries may then break as the Entity type no longer exists. So in my case Products is base type PadMain inherits from this. Used to have PadMains now PadMain EntityName is Products, so query on PadMains needs to change. From/To:
From b In context.PadMains
From b in context.Products1.OfType(Of PadMain)()
However if PadMain has subtypes then the SQL generated is verbose. If table is separated out in SQL then you just want the base table. If a field will appear in multiple related child tables then you will get duplications. In this scenario this is a major disadvantage of Entity Framework. Hopefully it will be fixed in future. For now I have removed my inheritance.