Association between Derived Types
Hi,
We have to model Student and Advisor in our project. The basic database diagram looks like this:
(we use the title as prefix of advisor's name, e.g.: Professor, Assistant Prof., ...)
Person
| Student
| Advisor
|
PersonID
| PersonID
| PersonID
|
Name
| StudentNumber | EmpCode |
| Surname | AdvisorID
|
|
The basic class diagram similar to this:
Person
| Student
| Advisor
|
PersonID
| StudentNumber | EmpCode |
Name
| Advisor | Students
|
| Surname |
|
|
The problem is, i can't model this diagram in EDM. Student can inherit from Person, Advisor can inherit from person but i can't associate Student with Advisor. If i use Table per Type Inheritance, i can't define Student as EntitySet, because i must define its BaseType, Person as EntitySet. Advisor also same. But Association requires two EntitySets to associate Student and Advisor.
How can i associate them?
Thanks.
[1586 byte] By [
hylegoz] at [2008-1-10]
Which version of the entity framework are you using? In the June ('07) CTP and in Beta 2, associations between subtypes (aka derived types) are fully supported. Previous versions required that the types that are associated be the base types of an entityset, but in these (and all future versions) you can specify types for the association which are derived from the entityset's base type.
When you define the association type you list the exact types that you are using (in your case Student and Advisor). When you define the association set you list the entitysets that those types are a member of (in your case both ends are parts of the Person entityset).
There are issues with the particular type of inheritance you use, though, when it comes time to map this to the database, and quite honestly I can't remember exactly what they are. It may be that the problem you are running into is related to that (given that you are using TPT inheritance). I know that this works with TPH inheritance (because I have an app that has a number of relationships between subtypes and uses TPH extensively).
- Danny
Daniel Simmons,
Please allow me to ask a follow up question as I've been looking at the same.
It's correct that a given instance of an EntityType may be a member of only one EntitySet isn't it?
More precisely, is it correct that it's not possible to have an EntitySet for BaseEntityType and for instance two separate EntitySets for DerivedEntityType1 and DerivedEntityType2? (I would very much like to do that, or do you have a convenient way to do what I'm obviously seeking?)
Best regards,
Henrik Dahl
You can definitely have EntitySets for multiple types in hierarchy. There are some restrictions around how you map these entity sets. You can not map these EntitySets to a common table
But EF designer does not support this feature. I beleive you can only have one EntitySet per type hierarchy if you are using designer.
Thanks
Srikanth.
If you set aside the designer for the moment, the one good way to think about EntitySets is as though they are tables or maybe even lists of a particular base type. They can contain entities of any type which inherits from the base type, but there's nothing which prevents you from having either multiple lists of the same basetype--just with different names--or even one list of base type A (call it listOfA) and another list which is restricted to base type B which inherits from A (call it listOfB). This doesn't mean that instances of B automatically end up in listOfB. You could put them in listOfA, but it does mean that you can't have an A in listOfB because it's type isn't compatible. The runtime APIs require that you specify the entityset when you add something, and when you query, the entityset you retrieve from is tracked as part of the key of the entity.
As Srikanth points out, the designer for the first release makes the simplifying restriction/assumption that you will have only one entityset for a particular type hierarchy which works for many cases, but not all of them.
- Danny