In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How does LINQ to SQL delete data? in view of this problem, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a simpler and easier way.
After learning about the query of LINQ to SQL and the implementation of adding and updating, let's take a look at how LINQ to SQL deletes data, and what are the specific implementation processes and steps? Let's see.
LINQ to SQL deletes data using Northwind as an example:
1. First, take a row of data in the Customers table as an example to delete LINQ to SQL:
NorthwindDataContext ctx = new NorthwindDataContext (); Customer test1 = ctx.Customers.Single (c = > c.CustomerID = = "TEST1"); ctx.Customers.Remove (test1); ctx.SubmitChanges ()
2. By looking at the Customers table in the database, we can find that the piece of data has been deleted.
NorthwindDataContext ctx = new NorthwindDataContext (); Customer test1 = ctx.Customers.Single (c = > c.CustomerID = = "TEST1"); ctx.Customers.Remove (test1); ctx.SubmitChanges (); test1 = ctx.Customers.Single (c = > c.CustomerID = = "TEST1"); Console.WriteLine (test1.CustomerID)
First delete a row of data whose CustomerID is "TEST1", and then query the data in the database. Theoretically, the data no longer exists in the database, and the query should have no results. But the screen output is "TEST1", which is the CustomerID of the Customer that has been deleted. Is it strange that the data in the database no longer exists, but the query can still get the correct results. In fact, the reason is also very simple. Although the data has been deleted in the database, the Identity Cache in DataContext still keeps a reference to the object (what is Identity Cache, which has been explained earlier), and the query results are the objects that are Cache in DataContext rather than in the database. You can know that if you query this data in another DataContext, it will definitely not be available.
3. Cascading deletions in LINQ to SQL deletions. Take Customers and Orders as examples:
NorthwindDataContext ctx = new NorthwindDataContext (); Customer test1 = ctx.Customers.Single (c = > c.CustomerID = = "TEST1"); Order order1 = test1.Orders.Single (o = > o.ShipCity = = "Shanghai"); test1.Orders.Remove (order1); ctx.SubmitChanges ()
In this example, you want to delete an order in which the Customer whose CustomerID is "TEST1" has the ShipCity of Shanghai. Execute this code, and you can see through SQL Profile that delete from Orders... is not running Instead of update, the SQL statement sets the CustomerID of the record in the Orders table to NULL, deleting the relationship between the record and Customer, but not actually deleting the record. To actually delete the record, you must do it through DataContext:
Ctx.Orders.Remove (order1); ctx.SubmitChanges ()
This is a problem worth paying attention to during the deletion process.
To delete Customer and associated Order, you should do this (you can also set cascading deletions in the database):
NorthwindDataContext ctx = new NorthwindDataContext (); Customer test1 = ctx.Customers.Single (c = > c.CustomerID = = "TEST1"); foreach (Order o in test1.Orders) {test1.Orders.Remove (o); ctx.Orders.Remove (o);} ctx.Customers.Remove (test1); ctx.SubmitChanges ()
In the data deletion will also encounter the same conflicts as data updates, the solution is basically the same, I will not say much here.
The answer to the question about how LINQ to SQL deletes data is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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: 0
*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.