Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity framework database first - Table per hierarchy (TPH) recursive relationship implementation

I am trying to implement TPH recursive relationship on one of the concrete types using Entity Framework 5 and Database first approach. I have conceptual model, and table structure like this:

Conceptual modelTable structure

Also, i have recursive relationship like this in my database table.

ALTER TABLE [dbo].[BaseType]  
WITH CHECK ADD  CONSTRAINT [FK_BaseType_DerivedType] 
FOREIGN KEY([Derived1RecursiveId])
REFERENCES [dbo].[BaseType] ([Id])

When i update model with this relation i get diagram like this: Problem - Model

My question is:

How can i implement a recursive relationship in a database so that when model is updated from database (refreshed), recursive relationship is set on DerivedType1?

like image 436
Olinad Avatar asked Mar 09 '14 16:03

Olinad


1 Answers

It is not possible for the EF model to automatically deduce that the Derived1RecourseiveId is part of the Derived1 class. But you can move the association in your edmx file manually. Just move the foreign key column, delete the association on your base table and recreate it on the derived. Once moved it will stay in your derived type even if you do a model refresh.

like image 100
codeworx Avatar answered Oct 22 '22 18:10

codeworx