none
How to External Map Many to Many relationship??? RRS feed

  • Question

  •  

    I have 3 tables ( Report, Category, and ReportCategory). My question is how I can map these associations in my mapping file. Basically I want the Category to have a Reports property that is of type IList<Report>.

     

    I would like to avoid creating a ReportCategory class if at all possible. Any ideas on how to implement this? Maybe some links to other examples?

     

    By the way I am using one mapping file to map all my entities as this is a fairly small project.

    Any help is appreciated.

     

    Thanks In Advance!

    Tuesday, November 18, 2008 8:08 PM

Answers

  • LINQ to SQL does not support many-to-many mapping.  You have to expose the intermediate table. 

     

    It is possible to make additional properties on your object that appear to be the collections you want, but only just so.  You'll still have to deal with the intermediate table when it comes to querying & updates.

     

    public class Category {

       ...

       public List<ReportCategory> ReportCategories { get; set; }

     

       public IEnumerable<Report> Reports {

            get { return this.ReportCategories.SelectMany(rc => rc.Reports); }

       }

    }

     

     

     

    Friday, November 21, 2008 4:23 AM
    Moderator

All replies

  • LINQ to SQL does not support many-to-many mapping.  You have to expose the intermediate table. 

     

    It is possible to make additional properties on your object that appear to be the collections you want, but only just so.  You'll still have to deal with the intermediate table when it comes to querying & updates.

     

    public class Category {

       ...

       public List<ReportCategory> ReportCategories { get; set; }

     

       public IEnumerable<Report> Reports {

            get { return this.ReportCategories.SelectMany(rc => rc.Reports); }

       }

    }

     

     

     

    Friday, November 21, 2008 4:23 AM
    Moderator
  • How do you deal with the intermediate table??? I have this problem. My thread:

    http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/ed37c355-0b8b-4f12-8261-3950d75de361
    Wednesday, January 7, 2009 4:21 PM