LINQ to SQL Instantiation
page 2 of 8
by Brian Mains
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 40937/ 55

Business Object Assignments

LINQ maintains all of its relationships between all the related objects. Every primary and foreign key relationship contains an object reference for that particular relationship. A PK reference is represented by an EntityRef generic object, while an FK reference is a collection of objects in an EntitySet generic collection.

Generally, LINQ-to-SQL works like most business objects you would expect to; if you assign the reference for that property to the business object, it retains that reference. However if you do not, it is null. Let me explain.  Let us say the data context has an Orders table. This Orders table is related to the Customers table, where the Customer is the primary key. For an order record in that table, which is represented by the Order business object, this object has a Customer reference property and a CustomerKey GUID property.

If you assign a value to the CustomerKey, the Customer object reference is not automatically populated; however, the reverse is not true. Whenever an object reference is provided to the Customer property, the CustomerKey property contains the GUID primary key value.

Once the call to the DataContext.SubmitChanges is made, the value assigned to CustomerKey refreshes the value in the Customer object reference property. Let us look at an example. Below a new order object is created and a customer key is assigned.

Listing 1

Order order = new Order();
 order.CustomerKey = customerKey;
Assert.IsNotNull(order.Customer);  // is success

But, if using the opposite approach where an object is assigned to the Customer property, the CustomerKey is populated.

Listing 2

Order order = new Order();
 order.Customer = context.Customers.First();
 Assert.IsTrue(order.CustomerKey != Guid.Empty); // is success

If you are updating your LINQ objects, and you want to update an object without submitting the changes to the database at that moment, you can make the assignment via an object reference, which will update the relationship key.


View Entire Article

User Comments

Title: Another way to do this...?   
Name: Funka!
Date: 2009-08-14 5:17:13 PM
Comment:
Another possibility it seems is to create a partial class and partial method "OnCreated" ?
For example:
public partial class Order
{
partial void OnCreated()
{
_OrderKey = Guid.NewGuid();
_CreatedDate = DateTime.Now;
}
}
This way, you don't need to keep remembering to do this every time you instantiate an object.

It should also be noted that you avoid your specific error (but not the general need for setting default values) by setting the LTS entity property's "Auto Generated Value" to true --- LTS will know not to supply a value to these columns when inserting, so you can just let SQL server fill in these defaults.

Thanks!
-f!
Title: Great Article !!!   
Name: Jash
Date: 2009-04-13 3:24:14 PM
Comment:
Thank you Brian really useful article.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-26 3:09:15 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search