简体   繁体   English

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

[英]LINQ to SQL update entity that came from client side

I have entity (LONGFORM) that is passed via AJAX to client side. 我有通过AJAX传递到客户端的实体(LONGFORM)。 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! 但是我无法将_lfFromClient(LONGFORM)“更新”回数据库! 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). 如果我使用InsertOnSubmit,那么将插入记录(尽管LINQ应该看到表中已经存在PK字段并尝试更新)。 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? 那么,更新与当前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); 编辑:我设法更新LONGFORM中的值,在SubmitChanges()之前添加此行:_db.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues,_lfFromClient);

Now the problems is that child entities inside _lfFromClient wont get updated :( 现在的问题是_lfFromClient中的子实体不会得到更新:(

EDIT 2: Ok, I found the solution so here's the answer in hopes it will help someone with the same problem. 编辑2:好的,我找到了解决方案,所以这里是答案,希望它可以帮助遇到相同问题的人。 The trick is to also attach all the child entities because LINQ wont do it automatically. 诀窍是还附加所有子实体,因为LINQ不会自动执行。 In this example FAMILYMEMBER is a child entity collection of LONGFORM that also gets update on client side. 在此示例中,FAMILYMEMBER是LONGFORM的子实体集合,该子实体集合也在客户端得到更新。 Note the "AttachAll" since LONGFORM -> FAMILYMEMBER is a one to many relation: 请注意“ AttachAll”,因为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();

    }

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: 编辑:如果您希望使用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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM