0

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();

    }
Nick
  • 727
  • 10
  • 20

1 Answers1

0

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();
}
Community
  • 1
  • 1
alf
  • 18,372
  • 10
  • 61
  • 92
  • I tried and it says: An entity can only be attached as modified without original state if it declares a version member or does not have an update check policy. – Nick Aug 24 '11 at 01:02
  • I just edited my answer. Check out that related question. Hope it helps! – alf Aug 24 '11 at 01:03
  • Ok, the suggested link gives 2 options. I like to try the second, so, how do I "Alternatively, you could also grab the entity from the db and change it using the data from the POSTed entity, then submit that as a change." ??? I mean, I have _lfFromClient and I can also generate another entity, but how do I replace the values in the new entity with the values in _lfFromClient? – Nick Aug 24 '11 at 01:10
  • Find the relevant entity with `_db.LONGFORMs.Where(l => l.ID == _lfFromClient.ID).FirstOrDefault()`. Then update all the fields of that entity based on the fields of _lfFromClient. – alf Aug 24 '11 at 01:17
  • Yeah I tried, but it says: "Cannot add an entity with a key that is already in use." Since both entity obviously have the same PK – Nick Aug 24 '11 at 01:22
  • Just retrieve the entity, update it, and do a `_db.SubmitChanges()`. Do not perform any inserts or attachs. – alf Aug 24 '11 at 01:25
  • I think you are missing the whole problem here... the entity is DETACHED from the context since it goes to the client and back. I would love to just do SubmitChanges! – Nick Aug 24 '11 at 01:28
  • But the entity you are grabbing from the DB is not detached. I just added an example to my answer. – alf Aug 24 '11 at 01:37