Derived Associations in .Net Entity Framework
I want to have a single table that represents a person and have a number of other tables (such as Student/Teacher) use the Person table to store information related to a person. Unfortunately the entity framework doesn't seem to like it when I try to add an association between the Student or Teacher class and I don't understand why. The Person table contains a column called ParentEntityID, which equals either a StudentEntityID or a TeacherEntityID. In an ideal world I would like to be able to reference the Person table by going Student.Person.FirstName instead of Student.Entity.Person1.FirstName. The error .Net returns when trying to connect the Student/Teacher table to the Person table is:
Error 3007: Problem in Mapping Fragments starting at lines 265, 289: Non-Primary-Key column(s) [ParentEntityID] are being mapped in both fragments to different conceptual side properties - data inconsistency is possible because the corresponding conceptual side properties can be independently modified.
The actual database I am working with is a bit more complicated and has a number of tables connected to the Entity table, which makes my object model pretty ambiguous and was hoping to be able to clean it up a bit by using the entity framework.
I am not much of a database guru, normally I work on the front end... Can what I want be done or is there a better solution?
I am using VB.Net 3.5 in a web application.
alt text http://commonfish.com/images/2010-stackoverflow1.png
Update...
In an attempt to internalize the different types of relationships I can form, I have been playing with this simple example and have come up with the following possibilities: alt text http://commonfish.com/images/2010-stackoverflow2.png
alt text http://commonfish.com/images/2010-stackoverflow3.png
And the reason why I cannot make an association between the Student and Address table is because that relationship is not enforced in the database. If I wanted an association between these two tables I would need to re-think the design of my database. Is that correct? What I want to do is not supported by the Entity Framework?
Also, looking at a page linked from Mr. Pratt, it sounds like .Net 4 supports foreign key associations, which I think is wh开发者_如何学Goat I am trying to do. Am I interpreting that article correctly??
I believe you are getting an error because entity framework wants just one relationship/navigation property to own or manage the 'ParentEntityID' property. If, for example, you were to set both the 'Teacher' and 'Student' navigation properties on a 'Person' entity, EF wouldn't know which one should 'win'.
Can a single 'Person' be both a 'Student' and a 'Teacher'? If not, I would suggest modeling this as both 'Student' and 'Teacher' entities inheriting from 'Person' via TPT (table-per-type) inheritance (example).
精彩评论