Get the App
SLTechnology News&Howtos  ›  Development  › 

What's the use of Linq DataContext?

Shulou Source: shulou.com Published: 2022-06-02 09:02:11 09月13日 Update

This article mainly introduces the use of Linq DataContext, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Primary key cache

Linq to sql caches the queried object and then reads it directly from the cache if only one record is queried based on the primary key. For example, the following code:

Customer C1 = ctx.Customers.Single (customer = > customer.CustomerID = = "ANATR"); c1.ContactName = "zhuye"; Customer c2 = ctx.Customers.Single (customer = > customer.CustomerID = = "ANATR"); Response.Write (c2.ContactName)

Only one SQL will be generated after execution:

SELECT [t0]. [CustomerID], [t0]. [CompanyName], [t0]. [ContactName], [t0].

[ContactTitle], [t0]. [Address], [t0]. [City], [t0]. [Region], [t0].

[PostalCode], [t0]. [Country], [t0]. [Phone], [t0]. [Fax]

FROM [dbo]. [Customers] AS [t0]

WHERE [t0]. [CustomerID] = @ p0

-- @ p0: Input String (Size = 5; Prec = 0; Scale = 0) [ANATR]

Because the changes are not committed, the records in the database are still not updated. Because of this feature, we should be careful when using stored procedures as entity update methods. Stored procedures are miswritten, and even if you submit changes, it is likely to cause inconsistency between the data in the cache and the data in the database. Cause unnecessary trouble.

Linq DataContext isolation

Sometimes we will pass the object into the Linq DataContext from the outside and ask it to be updated, because different Linq DataContext is relatively independent. Since no entities have been acquired in the new Linq DataContext, we can only update the data in an additional way.

First, add the IsVersion logo to the primary key field of the Customer table:

[Column (Storage= "_ CustomerID", DbType= "NChar (5) NOT NULL"

CanBeNull=false, IsPrimaryKey=true, IsVersion = true)]

Run the following test code:

Customer c = new Customer {CustomerID = "ALFKI"

ContactName = "zhuye", CompanyName = "1111"}

Ctx.Customers.Attach (c, true)

Ctx.SubmitChanges ()

The following SQL statement is captured:

UPDATE [dbo]. [Customers]

SET [CompanyName] = @ p2, [ContactName] = @ p3, [ContactTitle] = @ p4

[Address] = @ p5, [City] = @ p6, [Region] = @ p7, [PostalCode] = @ p8

[Country] = @ p9, [Phone] = @ p10, [Fax] = @ p11

WHERE ([CustomerID] = @ p0) AND ([CustomerID] = @ p1)

-- @ p0: Input StringFixedLength (Size = 5; Prec = 0; Scale = 0) [ALFKI]

-- @ p1: Input String (Size = 5; Prec = 0; Scale = 0) [ALFKI]

-- @ p2: Input String (Size = 4; Prec = 0; Scale = 0) [1111]

-- @ p3: Input String (Size = 5; Prec = 0; Scale = 0) [zhuye]

-- @ p4: Input String (Size = 0; Prec = 0; Scale = 0) []

-- @ P5: Input String (Size = 0; Prec = 0; Scale = 0) []

-- @ p6: Input String (Size = 0; Prec = 0; Scale = 0) []

-- @ p7: Input String (Size = 0; Prec = 0; Scale = 0) []

-- @ p8: Input String (Size = 0; Prec = 0; Scale = 0) []

-- @ p9: Input String (Size = 0; Prec = 0; Scale = 0) []

-- @ p10: Input String (Size = 0; Prec = 0; Scale = 0) []

-- @ p11: Input String (Size = 0; Prec = 0; Scale = 0) []

Thank you for reading this article carefully. I hope the article "what's the use of Linq DataContext" shared by the editor will be helpful to everyone? at the same time, I also hope that you will support and pay attention to the industry information channel, and more related knowledge is waiting for you to learn!

Tags: Data article cache update facet code entity object database time procedure storage query difference consistency value interest at the same time field method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Linux macOS NVidia Redmi