New Posts New Posts RSS Feed: Table Inheritance
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Table Inheritance

 Post Reply Post Reply
Author
mikewishart View Drop Down
Groupie
Groupie
Avatar

Joined: 26-Feb-2010
Location: Reno, NV
Posts: 49
Post Options Post Options   Quote mikewishart Quote  Post ReplyReply Direct Link To This Post Topic: Table Inheritance
    Posted: 13-Sep-2010 at 2:37pm
Is there an example of how to use the (new?) "Base Type" property for an entity?

We're trying to do something along the lines of:

Person

(pk)PersonId
Name
Address
City

Employee -> Person
(pk)PersonId
StartDate
Status

I can't see how to get around the Employee having the same primary key as the Person if you set Person as the base type for Employee.  The Employee table requires the field for the relationship to work.

Thanks!
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 667
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 14-Sep-2010 at 11:58am
Hi Mike;

Overriding a property of a generated DevForce entity via standard code generation is not possible. You might be able to do it by implementing a custom .tt template but other DevForce features might break.
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 667
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 14-Sep-2010 at 12:46pm
My other suggestion is to look at our PropertyInterceptors and see if it can help you do what you're trying to achieve.

PropertyInterceptors are "is intended to replace, and expand upon, the technique of marking properties as virtual and overriding them in a subclass".

Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1278
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 14-Sep-2010 at 4:12pm
We don't currently have any samples in the DevForce Resource Center showing inheritance, but we should have some samples lying around which we can send you.  The BaseType isn't new, since inheritance was supported in EF v1, and by extension DevForce.  We support everything EF does regarding the three "flavors" of inheritance based on your DB schema:  Table per Type (TPT), Table per Hierarchy (TPH) and Table per Concrete type (TPC).   These all expect the PK to be the same, so I'm not sure if you're running into a problem or just working through the design conceptually.  
 
I'm trying to find a good inheritance write up from Microsoft and not seeing much, but here are a few links to provide an overview:
 
If you'd like a sample showing a specific flavor of inheritance just let us know.
Back to Top
mikewishart View Drop Down
Groupie
Groupie
Avatar

Joined: 26-Feb-2010
Location: Reno, NV
Posts: 49
Post Options Post Options   Quote mikewishart Quote  Post ReplyReply Direct Link To This Post Posted: 14-Sep-2010 at 5:16pm
Thanks!  We're looking at a Table-per-Type architecture here.

Kim, your second link was extremely helpful and almost got it.  I was missing the step of deleting the primary key columns from the mapping on the inherited tables.

One more item to stump me.  If we have a common column between the two tables, say something we use for replication (eg. LastUpdateTime), it can certainly be the same value in both tables, but I don't see any way to map the property mapping in the descendant table to the parent table.  I suppose I can rename it (LastUpdateTimeTPT) and set it to the same value and change its setter/getter to private, but that's not the most perfect solution. If that's what I need to do, then I'll look at that.

Looking ahead a bit.  When I get to the EntityServerSaveInterceptor.ExecuteSave(), should I be concerned about anything. Any pitfalls here?

Thanks again!
Mike
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1278
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 14-Sep-2010 at 7:31pm
I don't have a good answer for the LastUpdateTime columns.  That might be a good question for the EF forum on MSDN (I'm really not trying to dodge the question, but best to ask the experts)  - http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/threads/
 
You shouldn't have a problem with an ExecuteSave override, unless you're really trying to take control of the save process, which you shouldn't.   You'll be able to retrieve entities OfType<Person> or OfType<Employee> and work with them as you normally would.  The one thing that can sometimes be confusing when working with inherited entities is that the EntityQuery auto-generated on your EntityManager will return entities of the type and all base types, so entityManager.Persons will return any type of person.
Back to Top
mseeli View Drop Down
Newbie
Newbie
Avatar

Joined: 20-Sep-2010
Location: Switzerland
Posts: 31
Post Options Post Options   Quote mseeli Quote  Post ReplyReply Direct Link To This Post Posted: 20-Sep-2010 at 6:36am
I am trying to do the following using inheritance (table by hierarchy):
 
I have a Person and derived from that two Subclasses:  Student and Teacher
 
Now I have a further class called Class with an association between Student and Class (i.e. a Student belongs to a Class, a Class has many Students in it but a Teacher is not in a Class.
 
So the navigational property from StudentToClass should be on the Student but not on Person or Teacher.
 
I have tried for hours to get the mapping to work but I could not get it to work.
 
Is what I am trying possible or does DefForce not support what I am trying to achieve?
 
Markus Seeli
Back to Top
mikewishart View Drop Down
Groupie
Groupie
Avatar

Joined: 26-Feb-2010
Location: Reno, NV
Posts: 49
Post Options Post Options   Quote mikewishart Quote  Post ReplyReply Direct Link To This Post Posted: 20-Sep-2010 at 3:46pm
Kim - Thank you!  I'll see if we go that route.

Markus,

As I understand it, this is what you're looking for in EDMX.


Your physical database needs a table between Student and Class and needs to look like this.



Once I built the data schema then imported all 5 tables into the EDMX, it worked just fine.  Of course I had to delete the FK relationships between Person-Teacher and Person-Student and delete the PersonId on Student and Teacher.





Edited by mikewishart - 20-Sep-2010 at 3:57pm
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down