且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在不先检索行的情况下使用Linq to Entities从我的表中删除一个或多个行?

更新时间:2023-09-10 12:23:34

是的,您可以执行此操作.来自此提示:

// Create an entity to represent the Entity you wish to delete
// Notice you don't need to know all the properties, in this
// case just the ID will do.
Category stub = new Category { ID = 4 };
// Now attach the category stub object to the "Categories" set.
// This puts the Entity into the context in the unchanged state,
// This is same state it would have had if you made the query
ctx.AttachTo("Categories", stub);
// Do the delete the category
ctx.DeleteObject(stub);
// Apply the delete to the database
ctx.SaveChanges();

请参见解决方案