LINQ to SQL 之 使用Attach 和 解析Table<T>.Attach引发的异常和解决方法 (转)

使用Attach更新(Update with Attach)

说明:在对于在不同的DataContext之间,使用Attach方法来更新数据。例如在一个名为tempdb的NorthwindDataContext中,查询出Customer和Order,在另一个NorthwindDataContext中,Customer的地址更新为123 First Ave,Order的CustomerID 更新为CHOPS。

//通常,通过从其他层反序列化 XML 来获取要附加的实体
//不支持将实体从一个DataContext附加到另一个DataContext
//因此若要复制反序列化实体的操作,将在此处重新创建这些实体
Customer c1;
List<Order> deserializedOrders = new List<Order>();
Customer deserializedC1;
using (NorthwindDataContext tempdb = new NorthwindDataContext())
{
    c1 = tempdb.Customers.Single(c => c.CustomerID == "ALFKI");
    deserializedC1 = new Customer
    {
        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 o in tempcust.Orders)
    {
        deserializedOrders.Add(new Order
        {
            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 = new NorthwindDataContext())
{
    //将第一个实体附加到当前数据上下文,以跟踪更改
    //对Customer更新,不能写错
    db2.Customers.Attach(deserializedC1);
    //更改所跟踪的实体
    deserializedC1.Address = "123 First Ave";
    //附加订单列表中的所有实体
    db2.Orders.AttachAll(deserializedOrders);
    //将订单更新为属于其他客户
    foreach (Order o in deserializedOrders)
    {
        o.CustomerID = "CHOPS";
    }
    //在当前数据上下文中提交更改
    db2.SubmitChanges();
}

语句描述:从另一个层中获取实体,使用Attach和AttachAll将反序列化后的实体附加到数据上下文,然后更新实体。更改被提交到数据库。

使用Attach更新和删除(Update and Delete with Attach)

说明:在不同的DataContext中,实现插入、更新、删除。看下面的一个例子:

//通常,通过从其他层反序列化XML获取要附加的实体
//此示例使用 LoadWith 在一个查询中预先加载客户和订单,
//并禁用延迟加载
Customer cust = null;
using (NorthwindDataContext tempdb = new NorthwindDataContext())
{
    DataLoadOptions shape = new DataLoadOptions();
    shape.LoadWith<Customer>(c => c.Orders);
    //加载第一个客户实体及其订单
    tempdb.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 = new NorthwindDataContext())
{
    //将第一个实体附加到当前数据上下文,以跟踪更改
    db2.Customers.Attach(cust);
    //附加相关订单以进行跟踪; 否则将在提交时插入它们
    db2.Orders.AttachAll(cust.Orders.ToList());
    //更新客户的Phone.
    cust.Phone = "2345 5436";
    //更新第一个订单OrderA的ShipCity.
    orderA.ShipCity = "Redmond";
    //移除第二个订单OrderB.
    cust.Orders.Remove(orderB);
    //添加一个新的订单Order到客户Customer中.
    Order orderC = new Order() { ShipCity = "New York" };
    cust.Orders.Add(orderC);
    //提交执行
    db2.SubmitChanges();
}

语句描述:从一个上下文提取实体,并使用 Attach 和 AttachAll 附加来自其他上下文的实体,然后更新这两个实体,删除一个实体,添加另一个实体。更改被提交到数据库。

出处:http://kb.cnblogs.com/page/42477/2/

参考:

LINQ那些事(9)-解析Table<T>.Attach引发的异常和解决方法http://www.cnblogs.com/chwkai/archive/2010/01/21/linq-sql-attach.html

posted @ 2014-07-17 11:03  邹邹  Views(233)  Comments(0)    收藏  举报