无法附加以存在的实体
private void Form1_Load(object sender, EventArgs e)
{
bool b = false;
Table<a> ldt;
ldt = Selectall();
foreach (a t in ldt)
{
t.c = "asdf";
b =Updater(a2);
if (b)
{
MessageBox.Show("修改成功 !");
}
else
{
MessageBox.Show("修改失败!");
}
}
}
public Table<a> Selectall()
{
try
{
return ddc.a;
}
catch (Exception ee)
{
throw ee;
}
}
public bool Updater(a ts)
{
try
{
ddc.a.Attach(ts);
ddc.Refresh(RefreshMode.KeepCurrentValues, ts);
ddc.SubmitChanges(ConflictMode.ContinueOnConflict);
return true;
}
catch(Exception e)
{
MessageBox.Show(e.Message);
return false;
}
}
运行后总提示修改不成功,无法附加以存在的实体。这是哪里错了?
------解决方案--------------------a a1;//实体
a1 = SelectSingle(); //假设查询返回实体而非实体集
a a2=new a();
a2.title =a1.title();
a2.content = "我爸李刚";
linqDataContext ddc=new LinqDateDataContext();
ddc.admin.Attach(a2);
ddc.Refresh(RefreshMode.KeepCurrentValues, a2);
ddc.SubmitChanges(ConflictMode.ContinueOnConflict);
------解决方案--------------------