LINQ to SQL 更新来自客户端的实体

发布于 2024-11-30 20:31:41 字数 1664 浏览 0 评论 0原文

我有通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪操控生活 2024-12-07 20:31:41

使用:

_db.LONGFORMs.Attach(_lfFromClient, true);

通过这种方式,您可以附加修改后的实体。
请参阅 Table.Attach

如果此方法给您带来任何问题,请查看这个问题

编辑:
如果您希望使用 POSTed 数据更新实体,您可以尝试以下操作:

[System.Web.Services.WebMethod()]
public static void SaveDataFromClient(LONGFORM _lfFromClient)
{
    DBDataContext _db = new DBDataContext();
    var _lfFromDB = _db.LONGFORMs.Where(l => l.ID == _lfFromClient.ID).FirstOrDefault();
    // Update all the properties of _lfFromDB here. For example:
    _lfFromDB.Property1 = _lfFromClient.Property1;
    _lfFromDB.Property2 = _lfFromClient.Property2;
    _db.SubmitChanges();
}

Use:

_db.LONGFORMs.Attach(_lfFromClient, true);

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:

[System.Web.Services.WebMethod()]
public static void SaveDataFromClient(LONGFORM _lfFromClient)
{
    DBDataContext _db = new DBDataContext();
    var _lfFromDB = _db.LONGFORMs.Where(l => l.ID == _lfFromClient.ID).FirstOrDefault();
    // Update all the properties of _lfFromDB here. For example:
    _lfFromDB.Property1 = _lfFromClient.Property1;
    _lfFromDB.Property2 = _lfFromClient.Property2;
    _db.SubmitChanges();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文