Linq to SQL / Linq to Entities可以生成MERGE语句吗?

时间:2010-06-29 18:50:26

标签: c# linq-to-sql linq-to-entities

寻找合并的INSERT / UPDATE / DELETE语句,MERGE正是我所需要的,但我似乎无法找到LINQ / SQL是否支持它(来自http://www.sqlbook.com/SQL-Server/SQL-MERGE-35.aspx

-- Merge order items into OrderItems table
MERGE INTO OrderItem As oi
USING @UpdatedItems ui
ON (oi.OrderID = ui.OrderID AND oi.ProductID = ui.ProductID)
WHEN MATCHED THEN
    UPDATE SET Quantity = ui.Quantity, UnitCost = ui.UnitCost
WHEN NOT MATCHED THEN
    INSERT (OrderID, ProductID, Quantity, UnitCost)
    VALUES (@OrderID, ui.ProductID, ui.Quantity, ui.UnitCost)
WHEN SOURCE NOT MATCHED THEN
    DELETE;

1 个答案:

答案 0 :(得分:2)

LINQ to SQL的当前版本未生成MERGE语句。您必须在代码中创建自定义Upsert方法才能执行Upsert