Delete and use attach of LINQ to SQL statements (12)

Source: Internet
Author: User
Tags db2

1. Simple Form

Note: You can call the DeleteOnSubmit method.

OrderDetail OrderDetail =    db. Orderdetails.first    10255);d B. Orderdetails.deleteonsubmit (OrderDetail);d B. SubmitChanges ();

Statement Description: Use the DeleteOnSubmit method to remove the OrderDetail object from the OrderDetail table. Call SubmitChanges to persist this deletion to the database.

2. One-to-many relationships

Note: Order and OrderDetail are a one-to-many relationship, first deleteonsubmit its orderdetail (multi-port), and second deleteonsubmit its order (one end). Because one end is the primary key.

varOrderDetails = fromOinchdb. OrderDetailswhereO.order.customerid = ="Warth"&&O.order.employeeid==3    Selecto;varOrder =    ( fromOinchdb. OrderswhereO.customerid = ="Warth"&& O.employeeid = =3     Selecto). First ();foreach(OrderDetail ODinchOrderDetails) {db. Orderdetails.deleteonsubmit (OD);} Db. Orders.deleteonsubmit (order);d B. SubmitChanges ();

Statement Description Statement Description: Removes the order and order detail objects from the order and Order Details table using the DeleteOnSubmit method. First, delete from order details and then delete from orders. Call SubmitChanges to persist this deletion to the database.

3. Inference deletion (inferred delete)

Description: Order with OrderDetail is a one-to-many relationship, in the above example, we all delete CustomerID for Warth and EmployeeID 3 data, then we do not have to delete all? For example, the order of OrderID is 10248 OrderDetail There are many, but we just delete the OrderDetail ProductID is 11. Then use the Remove method.

10248=     one); order. Orderdetails.remove (OD);d B. SubmitChanges ();

Statement Description: This example shows how the inferred delete causes the actual delete operation to occur on an entity object when it is removed from its entityset by a reference entity. The inference delete behavior occurs only if the entity's association map sets DeleteOnNull to True and Canbenull is false.

Using Attach updates (update with Attach)

Description: Use the Attach method to update data between different DataContext. For example, in a northwinddatacontext named Tempdb, the customer and order are queried, and in another NorthwindDataContext, the customer's address is updated to 123 first Ave, The Order's CustomerID is updated to chops.

//Typically, you get the entities you want to attach by deserializing the XML from another layer//attaching entities from one DataContext to another DataContext is not supported//so to replicate the operations of the deserialized entity, the entities are recreated hereCustomer C1; List<Order> deserializedorders =NewList<order>(); Customer deserializedC1;using(NorthwindDataContext tempdb =NewNorthwindDataContext ()) {C1= tempdb. Customers.single (c = C.customerid = ="ALFKI"); DeserializedC1=NewCustomer {Address=C1. Address, City=C1. City, CompanyName=C1.companyname, ContactName=C1. ContactName, ContactTitle=C1. ContactTitle, Country=C1. Country, CustomerID=C1. CustomerID, Fax=C1. Fax, Phone=C1. Phone, PostalCode=C1. PostalCode, Region=C1.    region}; Customer Tempcust=tempdb. Customers.single (c= = C.customerid = ="ANTON"); foreach(Order oinchTempcust. Orders) {Deserializedorders.add (NewOrder {CustomerID=O.customerid, EmployeeID=O.employeeid, Freight=O.freight, OrderDate=o.orderdate, OrderID=O.orderid, RequiredDate=o.requireddate, ShipAddress=o.shipaddress, ShipCity=o.shipcity, ShipName=O.shipname, ShipCountry=O.shipcountry, ShippedDate=o.shippeddate, Shippostalcode=O.shippostalcode, ShipRegion=o.shipregion, ShipVia=O.shipvia}); }}using(NorthwindDataContext DB2 =NewNorthwindDataContext ()) {    //attaches the first entity to the current data context to track changes//update to customer, cannot write wrongDB2.    Customers.attach (deserializedC1); //to change the entity being trackedDeserializedc1.address ="123 First Ave"; //all entities in the Attach order listDB2.    Orders.attachall (deserializedorders); //Update an order to belong to another customer    foreach(Order oinchdeserializedorders) {O.customerid="Chops"; }    //commit changes in the current data contextDB2. SubmitChanges ();}

Statement Description: Gets an entity from another layer, appends the deserialized entity to the data context using attach and Attachall, and then updates the entity. The changes are committed to the database.

Update and delete using Attach (update and delete with Attach)

Description: In different DataContext, implement INSERT, UPDATE, delete. Look at one of the following examples:

//Typically, the entities to be attached are obtained by deserializing the XML from another layer//This example uses LoadWith to preload customers and orders in a query.//and disable lazy loadingCustomer cust =NULL;using(NorthwindDataContext tempdb =NewNorthwindDataContext ()) {dataloadoptions shape=Newdataloadoptions (); Shape. LoadWith<Customer> (c =c.orders); //load the first customer entity and its ordersTempdb. Loadoptions =shape; Tempdb. Deferredloadingenabled=false; Cust= tempdb. Customers.first (x = X.customerid = ="ALFKI");} Order Ordera=Cust. Orders.first (); Order Orderb= Cust. Orders.first (x = x.orderid >Ordera.orderid);using(NorthwindDataContext DB2 =NewNorthwindDataContext ()) {    //attaches the first entity to the current data context to track changesDB2.    Customers.attach (Cust); //Attach related orders for tracking, otherwise they will be inserted at commit timeDB2. Orders.attachall (Cust.    Orders.tolist ()); //Update the customer's phone.Cust. Phone ="2345 5436"; //Update the ShipCity of the first order Ordera.Ordera.shipcity ="Redmond"; //Remove the second order Orderb.Cust.    Orders.remove (Orderb); //Add a new order to customer customers.Order Orderc =NewOrder () {ShipCity ="New York" }; Cust.    Orders.add (ORDERC); //Submit ExecutionDB2. SubmitChanges ();}

Statement Description: Extracts entities from one context and appends entities from other contexts using Attach and Attachall, then updates the two entities, deletes one entity, and adds another entity. The changes are committed to the database.

Delete and use attach of LINQ to SQL statements (12)

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.