locked
Inserting many to many related records RRS feed

  • Question

  • I have entity classes and everything that were generated by blinq from our existing database. With the tables that have many to many relationships, how do I perform inserts, updates, etc. easily? None of this was generated, and I'm not familiar enough with LINQ to know how to do it manually. Could someone post an example of how to deal with these relationships? Or at least point me in the right direction? Thanks in advance!
    Saturday, June 24, 2006 4:41 PM

Answers

  • I am assuming that you have a normalized schema with three tables for the many-to-many: one each for the entities and a "table in the middle" for FKs; e.g. if you have Employee:Project relationship, you have three tables - Employee, Project and EmployeeProject.

    Currently, many-to-many relationships are modeled with the "class in the middle". So effectively, in the object model, you don't have many-to-many. There are two separate relationships - 1:n and m:1 and can be dealt with as such.

    If you didn't get those two relationships, please let us know. There should be an explicit entity class corresponding to the table in the middle (e.g. a class for EmployeeProject table in the example above).

    HTH

    Dinesh

     

    Monday, June 26, 2006 1:34 AM
  • LINQ to SQL requires you to define the middle table as an entity.  The kinds of entities described in the document do not correspond to the same kinds of entities available with LINQ to SQL.  In the document, the middle table objects still exist and are exposed, the document just chooses not to call them entities.  You are given an example of representing order-detail records as elements of the overall order entity. This is known as a containment relationship, where the contained item does not have unique identity. LINQ to SQL only supports declaring association relationships.  Therefore all objects in the model are entities.

     

     

    Wednesday, December 5, 2007 2:53 AM

All replies

  • I am assuming that you have a normalized schema with three tables for the many-to-many: one each for the entities and a "table in the middle" for FKs; e.g. if you have Employee:Project relationship, you have three tables - Employee, Project and EmployeeProject.

    Currently, many-to-many relationships are modeled with the "class in the middle". So effectively, in the object model, you don't have many-to-many. There are two separate relationships - 1:n and m:1 and can be dealt with as such.

    If you didn't get those two relationships, please let us know. There should be an explicit entity class corresponding to the table in the middle (e.g. a class for EmployeeProject table in the example above).

    HTH

    Dinesh

     

    Monday, June 26, 2006 1:34 AM
  • Hi,

    Also just adding to the above - once you have the many-to-many schema you'll have to handle any cascading you want. i.e. if you delete the employee and as a result wish to delete any associated EmployeeProduct entities, then you'll need to manage this yourself.

    Hopefully I'm wrong but that's my experience so far... hoping Linq-Entities may sort this ?

    ian

    Tuesday, June 27, 2006 11:31 AM
  • Hi Dinesh,

     

    Would you give a sample code how to model many-to-many in C# object model?

     

    Here you suggest map the middle table to an explicit entity class. However, in the Microsoft guide, it said it shouldn't.

    http://msdn2.microsoft.com/en-us/library/ms978496.aspx.

     

    My project adds another flavor, 3 tables in many-to-many, Question, Answer, Response.

    Response is the middle table. 1-to-many between Question and Answer.

     

    Thanks much,

    Jia

    Jiayuan_Li@yahoo.com

    Tuesday, December 4, 2007 9:35 PM
  • LINQ to SQL requires you to define the middle table as an entity.  The kinds of entities described in the document do not correspond to the same kinds of entities available with LINQ to SQL.  In the document, the middle table objects still exist and are exposed, the document just chooses not to call them entities.  You are given an example of representing order-detail records as elements of the overall order entity. This is known as a containment relationship, where the contained item does not have unique identity. LINQ to SQL only supports declaring association relationships.  Therefore all objects in the model are entities.

     

     

    Wednesday, December 5, 2007 2:53 AM