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

寻找一个组合的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;

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

点赞