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

Automatic Primary Keys

SQL Server supports the auto-generation of primary key values through the identity setting.  When creating a new LINQ business object, the value of the primary key is zero; meaning no value has been assigned. Calling SubmitChanges on the DataContext refreshes this value with the auto-generated value assigned from the backend database. Let us take a look at an example.

When creating a new business object, the value of the primary key is illustrated below.

Listing 6

Product product = new Product();
product.Name = "Value";

//Assign remaining values.

Listing 7

Assert.AreEqual(0, product.ProductKey);

But after the call to submit changes, the value is updated to:

_context.SubmitChanges();
Assert.AreEqual(6, product.ProductKey);

The product key is assigned to an actual value after the product is refreshed.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 1 and 5 and type the answer here:

User Comments

No comments posted yet.






Ads Powered by Lake Quincy Media
Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 7/24/2008 9:46:41 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search