Click here to Skip to main content
15,868,141 members
Articles / Database Development
Article

LINQ to SQL Relationships

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL2 min read 7K   1  
LINQ to SQL is an interesting Object-Relational Mapper because it remembers the relationships objects have between each other.  As usual, LINQ to SQL

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

LINQ to SQL is an interesting Object-Relational Mapper because it remembers the relationships objects have between each other.  As usual, LINQ to SQL works like any ORM in that it has a designer that the developer can use to create the architecture that mimics the database structure.  LINQ to SQL manages the relationships one object has between the other.  For instance, whenever one object is added to a child collection of another, a PK/FK  relationship is established.  If, in another object, the application changes the primary key reference to something else, this change is made in the respective collections.

For instance, a Customer object has an EntitySet object (which is the type of object used to represent a child relationship) of Order objects.  The Customer is the PK in the database, and the Orders collection is the FK.  Anytime a new object is added to the Orders entity set, it's queued up to be inserted into the database, as well as being added to the collection.  So, in this way, LINQ to SQL manages the relationship between Customers and Orders.  This also works if you change the primary key reference to another object.  LINQ to SQL will transfer this object to the new parent's entity set collection.

When a new object is created, it doesn't immediately belong to the context.  For instance, look at this:

Order order = new Order();
order.Total = 19.99;
order.Date = DateTime.Now;

At this point, this object is independent and not a part of the context (it is not queued up for insertion).  Once you do:

customer.Orders.Add(order)

Where the customer object was retrieved from the database, this new order is queued for insertion and added as a relationship to the customer.  If you use an identity, the key value is set to zero, until you submit the changes (refreshed whenever the object is actually inserted).  At this point, relationships aren't validated; however, if there is a primary key/foreign key constraint error, this becomes known at the time SubmitChanges is called by a thrown exception.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

755 members

Comments and Discussions

 
-- There are no messages in this forum --