Model-First to .NET Entity Framework with multiple inheritance stratagies


#1

I am trying to design a Class Diagram with the intention of using it to generate and maintain the database, and generate .NET classes in Entity Framework (preferably Core, but it looks like it doesn’t support Model First no matter who’s third party tools you use). Our desired structure has pretty much every class inheriting from an abstract base class that itself has other abstract base classes. Sometimes we get to a concrete class, and we want everything to be Table per Concrete Class, so that all fields end up in a single table per sub-class. However, sometimes we get to a class that is still abstract, but all of it’s sub-classes are to be Table per Class Hierarchy, so there is a discriminator, and all sub-classes are in the same table.

Suppose you had an abstract class Thing, which had an abstract sub-class Mammal, which had concrete classes Cat and Human. Our system isn’t about Mammals, so the differences between different sub-classes are minimal. Sometimes I want a field in an Entity that can be any kind of Mammal, and sometimes I want one that can only be a Cat, so it makes sense to have all sub-classes of Mammal in one table. But we have other Things, like Vehicle and House, and we have lots of details about them that are different, and no sub-classes of them, and we never just reference a Thing. We want each one of those to be in their own table. In all cases, we don’t want a Thing table. All the fields of Thing are in all derived tables.

However, there is a NOTE in the Inheritance Strategies page (https://www.visual-paradigm.com/support/documents/vpuserguide/3563/3564/85388_differentinh.html) that says that is only possible in Java, and .NET can’t mix inheritance types “within a generalization hierarchy”. Am I reading that correctly? Does that phrase mean that what I’m trying to do won’t work? If so, is it something that is being worked on? Is there a workaround? Can it be overcome with some custom generation coding?


#2

Hi jcormier,

It is a limitation of NHibernate (the library used with generated ORM code).

It is possible to use different mapping strategies for different branches of the same inheritance hierarchy, and then make use of implicit polymorphism to achieve polymorphism across the whole hierarchy. However, NHibernate does not support mixing <subclass>, and <joined-subclass> and <union-subclass> mappings under the same root <class> element.

ref: http://nhibernate.info/doc/nhibernate-reference/inheritance.html


#3

I’m using Entity Framework, not NHibernate, . Does that mean I won’t run into this limitation?


#4

Visual Paradigm only supports generate code for NHibernate. The inheritance strategies limitation applies to this feature (Hibernate > Generate Code).
For other frameworks, you can only generate .NET code from your class model by Instant Generator.


#5

Rats, I could have sworn I read somewhere that Visual Paradigm supports Entity Framework. :frowning:


#6

From official or unofficial website?
The official feature page claimed support NHibernate:
https://www.visual-paradigm.com/features/hibernate-orm-tools/