LINQ to SQL inheritance and associations RRS feed

  • General discussion

  • Apologies if this has been asked before - I've had a (quick) look through forum and can't find anything. I'm trying to create a class model using single-table inheritance and also - at the same time - associate a sub-class with a super-class as a many-to-one relationship. That probably doesn't make much sense, and a picture speaks a thousand words, so here is the issue I'm coming up against: -!2FBCF8017242E87B!2908.entry

    Any advice greatly appreciated, but - from my hugely limited experience - it looks like an oversight in the L2S designer. Can anyone shed any light on this?


    Wednesday, January 21, 2009 7:34 PM

All replies

  • LINQ to SQL doesn't support multiple tables mapped into a single entity with (or without) inheritance. You can achieve this with the Entity Framework.

    For an example of implementing inheritance with LINQ to SQL (on an early CTP), see

    Jim Wooley - "LINQ In Action", The book is now available. Don't wait for the movie
    Thursday, January 22, 2009 3:08 AM
  • Jim

    Thanks for your response. I'm not quite sure I fully understand what you're saying. In my example, I have a single table mapping into three entities. This is supported by the L2S framework and I have it working fine in code. The problem is that the designer does not display base class properties when trying to create associations between the two subclasses. This is not actually anything to do with the L2S framework per se, as (I believe) I have managed to get it working by simply modifying the designer.cs - the problem is that the visual designer surface in L2S doesn't show those base class properties out of the box.

    If I've misunderstood what you're saying please let me know :-)
    Thursday, January 22, 2009 3:19 AM
  • I believe I misunderstood your question when you mentioned that there was a many to one relationship between the entities. Looking at your blog post, it is more clear what you are trying to achieve. It appears that you need to add an association between the ProjectManagerPersonId and the Person.PersonId. Once you do that, you should be able to use the association in your query as follows:

    var Query = from pm in dc.ProjectManagers  
                select pm.PersonId, DevCount = pm.Developers.Count(); 

    Jim Wooley - "LINQ In Action", The book is now available. Don't wait for the movie
    Thursday, January 22, 2009 3:33 AM
  • Correct. But, as I state in my blog post - the designer doesn't allow you to create that association!
    Thursday, January 22, 2009 3:34 AM