Click here to Skip to main content
13,146,014 members (42,531 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

3.2K views
1 bookmarked
Posted 11 Oct 2013

LINQ to SQL Relationships

, 11 Oct 2013
Rate this:
Please Sign up or sign in to vote.
LINQ to SQL is an interesting Object-Relational Mapper because it remembers the relationships objects have between each other.  As usual, LINQ to SQL

Editorial Note

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)

Share

About the Author

ASP.NET Community
United States United States
This member doesn't quite have enough reputation to be able to display their biography and homepage.
Group type: Collaborative Group

765 members


You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 11 Oct 2013
Article Copyright 2013 by ASP.NET Community
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid