In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 215
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.