Identification of LINQ to SQL objects

Source: Internet
Author: User
Tags anonymous shallow copy

A lot of my friends have told me that I want to write about LINQ to SQL or VS plug-ins. Although there are many LINQ to SQL books on the market, they are all about how to use, lack of depth. On the VS plug-in aspect of the book is also very shallow, according to the books made out of things, can only be student-level things, do not take shots. They think I have the ability to write well.

From the point of view of technical ability, it is true that there is no problem, but it is a time-consuming thing to speak well of a technology. I paid a lot, if you can not get the reader's approval, then the topic is not meaningful to write down. The topic is not to teach you how to use LINQ to SQL, but to let you understand the principles of LINQ to SQL, and for those who want to write ORM, you must not miss it. After writing the "Dig Into LINQ to SQL" series, the next series is "VS plug-in development", so if you want me to continue to write, please remember the point of recommendation. Your recommendation is the motivation that I write down.

Overview

With regard to the identity of the object, the simple point is that the primary key is the same object, and in the cache of the data context, there is only one. Data context, when loading data, after the object is created, a copy of the object (shallow copy) is cloned for each entity class object that is created. Remember this, we'll use it later, to save the initial value of the object, and when the object's property value is modified, the copy's property value is not changed. Note that only entity class objects create replicas of objects, and anonymous class objects do not generate replicas or entities. Let's look at the following code:

Example One

var c1 = db. Categories.single (o => o.categoryid = 1);
var c2 = db. Categories.single (o => o.categoryid = 1);

In Example one example, C1 and C2 are equal, let's look at one of the following examples:

Case II

var c1 = db. Categories.select (o => new {O.categoryid, o.categoryname}). Single (o => O.categoryid = 1);
var c2 = db. Categories.select (o => new {O.categoryid, o.categoryname}). Single (o => O.categoryid = 1);

In this example, C1 and C2 are not equal.

Why do anonymous objects not support object identities? Because the anonymous object does not necessarily have a primary key. Linq to SQL identifies an entity object through the primary key of the entity, and when the data is loaded from the database, the primary key is used to retrieve the existing object from the cache before the object is created. What kind of problem does this cause? Execute the following code.

var c1 = db. Categories.single (o => o.categoryid = 1);

Assuming that CategoryID is 1 CategoryName as "beverages", open the database and change the value to "New Name", as shown in the following figure:

Figure I

And then execute the following code:

var c2 = db. Categories.single (o => o.categoryid = 1);

At this time C2 modified C2. What is the value of CategoryName? Still for "beverages"! Because the second time the data is loaded, because the primary key (CategoryID) is already a "1" category, in the second load, the new object is no longer created to use the Category object that was previously created. So how do you use C2 's CategoryName value for the latest value, "New Name"? Call the Refresh method of the data context.

 Db. Refresh (refreshmode.overwritecurrentvalues, C2);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.