问题描述
是否可以让 LINQ to SQL 使用 PK 删除记录,而无需先加载记录?类似于NHibernate的代理对象功能?
Is it possible to get LINQ to SQL to delete a record using the PK, without loading the record first? Similar to NHibernate's proxy object functionality?
推荐答案
你应该可以这样做:
var person = new Person(); person.ID = someID; using (var context = new DataContext(connString)) { context.Persons.Attach(person, false); //attach is as unmodified context.Persons.DeleteOnSubmit(person); //remove it context.SubmitChanges(); //submit changes to db }