LINQ to SQL Relationships
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.
Revision number 1, Thursday, June 26, 2008 10:56:46 PM by bmains
You must Login to comment.
Revision #3
Mon, Feb 4, 2008 8:00 AM
by
|
LINQ to SQL
Start with listening to Anders talk about LINQ in Anders Hejlsberg on LINQ and Functional Programming this video and reading the MSDN Introduction to LINQ. If that's too long, perhaps the 5 Minute Intro is more your speed. There's also a good Channel
|
Revision #3
Sat, Oct 11, 2008 8:24 AM
by
|
LINQ
From MSDN: "The LINQ Project is a codename for a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations. It extends C# and Visual Basic with native language syntax for queries and provides class
|
Revision #4
Wed, Jul 2, 2008 6:08 PM
by
|
LINQ to Objects (and Everything)
The most hyped new feature in Visual Studio 2008 is LINQ, which stands for Language INtegrated Queries. It's awesome, and you'll be using it to talk to SQL, Objects and everything in between. Specifically to LINQ to Objects, this mechanism has the
|
New
Mon, Feb 4, 2008 8:00 AM
by
|
LINQDataSource
There's quite a few data source controls in ASP.NET and one that folks are increasingly excited about is the LINQDataSource. Blogs LINQ to SQL (Part 5 - Binding UI using the ASP:LinqDataSource Control) - This is the best place to start to get your head around
|