LINQ to SQL - Object update RRS feed

  • Question

  • Hello,

    I have some table with two fields - id (int) and name (nvarchar). I
    create dbml for this schema.
    After that I create data context, get from table one item and close it
    data context. I have some offline-item.
    After that I change it item and create new data context. To apply
    changes from my offline-item I try to attach it item to context and
    receive challenge: "An attempt has been made to Attach or Add an entity
    that is not new, perhaps having been loaded from another DataContext.  
    This is not supported.".

    To decide this issue LINQ2SQL offer overridden method Attach:
    Attach(entity, bool)
    Attach(entity, original entity)

    In first case LINQ to SQL uses timestamp field to track changes. Ok.

    It is impossible to understand how works second method, because it's
    throws exception (DuplicateKeyException) or nothing do.
    Can you explain me how it's works?

    Tuesday, July 22, 2008 11:24 AM