It seems impossible to map multiple classes to a single view when using abstract/sub-class mapping of the form:
<class name="I[Entity]" abstract="true">
<union-subclass name="[Entity]">
...
</union-subclass>
</class>
I ended up mapping directly to the concrete classes using the proprty-ref attribute which works correctly:
<class name="Game" table="vw_player_points">
<id name="Id" column="id">
<generator class="hilo"/>
</id>
<property not-null="false" name="GameId" column="gameid"/>
<bag name="Points">
<key column="gameid" property-ref="GameId"/>
<one-to-many class="PlayerPoints" not-found="ignore"/>
</bag>
</class>
<class name="PlayerPoints" table="vw_player_points">
<id name="Id" column="id">
<generator class="hilo"/>
</id>
<property not-null="false" name="PlayerId" column="playerid"/>
<property not-null="false" name="Points" column="points"/>
</class>
Perhaps an 'abstract' sub-mapping type would negate the use of the union-subclass hack mapping to interfaces.
I've found a work-around. It's a very obvious work-around that is non-ideal, but I think I'm going to call it good enough for us until EF5 comes out to fix this.
Short Answer: Just rename one or both of the ambigous entities Like: 2x Person
are renamed to: Personal_Person
and Work_Person
based on a PersonalContext
and a WorkContext
.
Long Answer: In our scenario, we're using a DB-first approach (we're rewriting a legacy app with minimal DB changes). Our DB contains hundreds of tables, so rather than using a single EDMX/Context, I'm using multiple EDMX/Contexts (the EDMX has croaked every time I've attempted to add more than half of our tables). However, some tables need to exist in more than one EDMX/Context.
For discussion, let's pretend we have a simple database with the following tables:
Person
Family
Relationship
Address
Business
Employee
Also, for the sake of this discussion, let's assume that ANY table that exists in multiple contexts causes this problem (as I stated in comments to Devart's answer, this is not really true and I don't understand why it sometimes works).
Now let's say we want to create two contexts:
PersonalContext
:
Person
Family
Relationship
Address
WorkContext
:
Person
Business
Address
Employee
In this scenario, both Person
and Address
will cause our problem. So what we will do in our EDMX mapping is simply rename our entities to Personal_Person
/Work_Person
and Personal_Address
/Work_Address
.
As stated, this is very much an obvious work-around that is non-ideal but since EF doesn't take namespacing into account and goes strictly by name (not true identity, simply the name), one option is to put your namespacing inside your name.
Now I'm still debating if I'm going to do it that way or perhaps namespace the name for every entity (Personal_Person
, Personal_Family
, Personal_Relationship
, Personal_Address
and Work_Person
, Work_Business
, Work_Address
, and Work_Employee
) for both consistency and Intellisense-friendliness (keeping all entities in proper alphabetic order) since really, the namespace belongs before the name instead of after it, but that's a judgement call and not really important to providing a solution to the problem.
I hope this helps!!
Best Answer
I found the answer on the Hibernate website:
I used that as an attribute for the
<hibernate-mapping>
tag and it worked.