Eric's Blog

Day to day experience in .NET
Welcome to Blogs @ IRM Sign in | Join | Help
 Search

Disclaimer

The content of this site is my own personal opinion and does not in any way represent my employer, it's subsideries or affiliates. These postings are provided "AS IS" with no warranties, and confer no rights.

This Blog

Association in Dervied Classes with LINQ to SQL

Look at the diagram above. There is a simple and common situation where a class Employee is derived from Person. In LINQ to SQL the key for the object is in the base class (in this case Person). Now, the Employee class wants to add an association to the Position class effectively creating a Positions property in the Employee class. The problem though is that when trying to create the association in the LINQ to SQL designer you will fail. The reason to this is bacuase the "Association Editor" will only allow you to select from properties in the classes where you create the association (in this case Employee and Position) and not from properties that are inherited from the Person class.

The solution to the problem I used was to add the association between the Person and the Position class instead. After doing that I closed the diagram and opened it up in a XML Editor and there manually move the association from the Person class to the Employee class and making some renames (changing Person to Employee). Also don't forget to go to the Position class in the XML and make the same rename changes there. After closing the file, right-clicking it in Solution Explorer and choosing "Run custom tool", the correct code is generated and the diagram correctly shows the association.

Published den 14 april 2008 08:59 by ericqu
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Steve Hobbs said:

You're a life saver! I don't know how long I've been searching for a work-around for what is essentially a bug with the O/R design surface. All we need now is a hotfix to sort this the proper way.
april 28, 2008 13:44
 

John Rusk said:

Thank you!!!! :-)
juni 11, 2008 00:28
 

Scott said:

Yes, well done. A lot of people seem to be after this solution!
september 21, 2008 07:17

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server, by Telligent Systems