开发者

Can Linq to SQL/Linq to Entities generate a MERGE statement?

开发者 https://www.devze.com 2023-01-05 16:18 出处:网络
Looking for a combined INSERT/UPDATE/DELETE statement, MERGE is exactly what I need, but I can\'t seem to find if LINQ/SQL supports it (from http://www.sqlbook.com/SQL-Server/SQL-MERGE-35.aspx)

Looking for a combined INSERT/UPDATE/DELETE statement, MERGE is exactly what I need, but I can't seem to find if LINQ/SQL supports it (from 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 开发者_Python百科NOT MATCHED THEN
    INSERT (OrderID, ProductID, Quantity, UnitCost)
    VALUES (@OrderID, ui.ProductID, ui.Quantity, ui.UnitCost)
WHEN SOURCE NOT MATCHED THEN
    DELETE;


Current version of LINQ to SQL does not generate a MERGE statement. You must create a custom Upsert method in code to do an Upsert.

0

精彩评论

暂无评论...
验证码 换一张
取 消