以下简化代码不起作用(因为它将检索到的对象引用设置为参数),但它显示了我想要做的事情
public bool updateEvent(clubEvent newEvent)
{
TNightCon tCon = new TNightCon();
clubEvent cEv = (from cEvent in tCon.clubEvents
where cEvent.EventID == newEvent.EventID
select cEvent).First();
// Won't work, but do I have to set all the fields manually?
cEv = newEvent;
tCon.SubmitChanges();
return true;
}
答案 0 :(得分:3)
或者,或者,
tCon.ClubEvents.DeleteOnSubmit(cEv);
tCon.CLubEvents.InsertOnSubmit(newEvent);
tCon.SubmitChanges();
答案 1 :(得分:2)
您无需检索当前对象即可执行您需要执行的操作。如果Ids是相同的,您需要做的就是将新对象附加到您的上下文。然后诀窍就是让Linq2SQL将对象视为“脏”。 Timothy Khouri有a blog post详细介绍了在上下文中使用Refresh方法的有用技巧。这就是它的样子。
public bool updateEvent(clubEvent newEvent)
{
tCon.clubEvents.Attach(newEvent);
tCon.Refresh(RefreshMode.KeepCurrentValues, settings)
tCon.SubmitChanges();
return true;
}