有能力做这项工作吗?我该怎么做才能做到这一点?
答案 0 :(得分:3)
当您使用DataContext生成LINQ to SQL查询时,它将跟踪从该上下文中发出的查询中选择的对象。
话虽如此,如果您对返回的对象进行更改,然后调用SubmitChanges method on the DataContext instance,则更改将保留回基础数据存储。
如果要删除对象,则将对象传递给DeleteOnSubmit method on the Table<T>
instance(其中T是数据库中表的模型类型)。然后,当您在DataContext上调用SubmitChanges时,传递给DeleteOnSubmit方法的模型所代表的记录将被删除。
答案 1 :(得分:3)
var context = new MyDataContext(); var newObj = new User(); newObj.UserID = 1; newObj.Name =“Ted”;
context.Users.InsertOnSubmit(newObj); //queues for submission
context.SubmitChanges(); //submits to backend
或更新:
var context = new MyDataContext();
var user = context.Users.First(i => i.UserID = 1);
//entities self aware and automatically synced to database when a value changes
user.Name = "Dave";
context.SubmitChanges(); //knows about updated record