c# – Polymorphic associations in LINQ to SQL – Education Career Blog

Does LINQ to SQL provide out-of-the-box polymorphic associations as ruby on rails active record does? If not, is there any workaround to manually map those associations?


Agreed. I found no possible way of doing this nor using the designer nor by hand appending class/method attributes. Moreover is not possible to have foreign key constraints for polymorphic associations. I discarded this option, thanks.



SQL Server won’t allow you to have a foreign key relationship on a column that is not a primary key or doesn’t have a unique constraint (or index) on it. There doesn’t seem to be any restriction on having multiple tables use the same column in the child table as the foreign key. The DBML designer does discover these relationships and will create associations to both parent tables when you import the table. It appears however, that the designer-generated code will only be generated for one of the associations. That is, the designer shows the associations properly, but the code for all but one of them is omitted. Further, the extensibility methods and property settors don’t seem to get defined properly in the designer-generated code either.

The same seems to be true if you add the associations by hand in the designer. Only one of the actual associations is implemented in the code and the other parent class’s code seems irretrievably broken. It’s possible that you may be able to use partial class implementations to add in the required functionality to match what the designer would generate, but I haven’t tried this.

Also, LINQ2SQL doesn’t support many-to-many relationships out of the box. You’re limited to 1-1 and 1-many without writing the code yourself.

Leave a Comment