LINQ to SQL 更新来自客户端的实体
我有通过 AJAX 传递到客户端的实体(LONGFORM)。该实体在客户端上进行修改,然后发送回服务器以在数据库中进行更新。这是服务器端代码:
[System.Web.Services.WebMethod()]
public static LONGFORM SendDataToClient(int id)
{
DBDataContext _db = new DBDataContext();
LONGFORM _lf = _db.LONGFORMs.SingleOrDefault(l => l.IDLONGFORM == id);
return _lf;
}
[System.Web.Services.WebMethod()]
public static void SaveDataFromClient(LONGFORM _lfFromClient)
{
DBDataContext _db = new DBDataContext();
//_db.LONGFORMs.InsertOnSubmit(_lfFromClient);
_db.LONGFORMs.Attach(_lfFromClient);
_db.SubmitChanges();
}
但我无法将 _lfFromClient (LONGFORM)“更新”回数据库! 如果我使用 InsertOnSubmit,记录将被插入(尽管 LINQ 应该看到表中已经存在 PK 字段,因此尝试更新)。如果我使用“附加”方法,什么也不会发生。
那么,更新与当前 DataContext 不相关的实体的正确方法是什么?
谢谢
。-编辑:我设法更新 LONGFORM 中的值,在 SubmitChanges() 之前添加此行: _db.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, _lfFromClient);
现在的问题是 _lfFromClient 内的子实体不会更新:(
编辑 2: 好的,我找到了解决方案,所以这里的答案希望它能帮助遇到同样问题的人。诀窍是也附加所有子实体,因为 LINQ 不会自动执行此操作,在此示例中,FAMILYMEMBER 是 LONGFORM 的子实体集合,它也会在客户端进行更新,因为 LONGFORM -> 。 FAMILYMEMBER 是一对多关系:
[System.Web.Services.WebMethod()]
public static void SaveData(LONGFORM _lfFromClient)
{
DBDataContext _db = new DBDataContext();
_db.LONGFORMs.Attach(_lfFromClient);
_db.FAMILYMEMBERs.AttachAll(_lfFromClient.FAMILYMEMBERs);
_db.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, _lfFromClient);
_db.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, _lfFromClient.FAMILYMEMBERs);
_db.SubmitChanges();
}
I have entity (LONGFORM) that is passed via AJAX to client side. This entity get modified on the client and it's sent back to the server for update in the database. Here's the server side code:
[System.Web.Services.WebMethod()]
public static LONGFORM SendDataToClient(int id)
{
DBDataContext _db = new DBDataContext();
LONGFORM _lf = _db.LONGFORMs.SingleOrDefault(l => l.IDLONGFORM == id);
return _lf;
}
[System.Web.Services.WebMethod()]
public static void SaveDataFromClient(LONGFORM _lfFromClient)
{
DBDataContext _db = new DBDataContext();
//_db.LONGFORMs.InsertOnSubmit(_lfFromClient);
_db.LONGFORMs.Attach(_lfFromClient);
_db.SubmitChanges();
}
But I can't "update" the _lfFromClient (LONGFORM) back into the DB!
If I use InsertOnSubmit the record will get inserted (despite the fact that LINQ should see that the PK field already exists in the table and thus try an update). If I use the "attach" approach nothing happens.
So, what is the correct way to update an entity that is not related to the current DataContext?
Thanks.-
EDIT: I managed to update the values in LONGFORM adding this line just before SubmitChanges():
_db.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, _lfFromClient);
Now the problems is that child entities inside _lfFromClient wont get updated :(
EDIT 2: Ok, I found the solution so here's the answer in hopes it will help someone with the same problem. The trick is to also attach all the child entities because LINQ wont do it automatically. In this example FAMILYMEMBER is a child entity collection of LONGFORM that also gets update on client side. Note the "AttachAll" since LONGFORM -> FAMILYMEMBER is a one to many relation:
[System.Web.Services.WebMethod()]
public static void SaveData(LONGFORM _lfFromClient)
{
DBDataContext _db = new DBDataContext();
_db.LONGFORMs.Attach(_lfFromClient);
_db.FAMILYMEMBERs.AttachAll(_lfFromClient.FAMILYMEMBERs);
_db.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, _lfFromClient);
_db.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, _lfFromClient.FAMILYMEMBERs);
_db.SubmitChanges();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用:
通过这种方式,您可以附加修改后的实体。
请参阅 Table.Attach
如果此方法给您带来任何问题,请查看这个问题。
编辑:
如果您希望使用 POSTed 数据更新实体,您可以尝试以下操作:
Use:
This way you are attaching the entity as modified.
See Table.Attach
If this approach gives you any problem, check out this question.
Edit:
If you prefer to update the entity with the POSTed data, you can try this: