2

我正在尝试更新Item其中一些成员已更改的内容,所以我正在这样做

public void Update(IItem pItem) {
    using(GameDBDataContext db = new GameDBDataContext()) {
        Item old = db.Items.SingleOrDefault(x => x.id == pItem.Id);
        if(old != null) {
            Item item = pItem as Item;

            old.id = item.id;
            old.type = item.type;
            old.owner_id = item.owner_id;
            old.player_id = item.player_id;
            old.currentdura = item.currentdura;
            old.maxdura = item.maxdura;
            old.location = item.location;
            old.socketprogress = item.socketprogress;
            old.firstsocket = item.firstsocket;
            old.secondsocket = item.secondsocket;
            old.effect = item.effect;
            old.composition = item.composition;
            old.bless = item.bless;
            old.isfree = item.isfree;
            old.enchant = item.enchant;
            old.suspicious = item.suspicious;
            old.islocked = item.islocked;
            old.color = item.color;
            old.compositionprogress = item.compositionprogress;
            old.inscribed = item.inscribed;
            old.inscribetime = item.inscribetime;
            old.amount = item.amount;

            db.SubmitChanges();
        }
    }
}

但是每次我尝试更新项目时都会抛出这个异常!

System.InvalidOperationException: Value of member 'location' of an object of typ
e 'Item' changed.
A member defining the identity of the object cannot be changed.
Consider adding a new object with new identity and deleting the existing one ins
tead.
   at System.Data.Linq.ChangeProcessor.CheckForInvalidChanges(TrackedObject trac
ked)
   at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
   at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
   at Database.Managers.ItemManager.Update(IItem pItem) in d:\CProgramming\DarkS
iders.Core\Database\Managers\ItemManager.cs:line 59

不知道这里出了什么问题:-s!?

4

1 回答 1

4

您正在尝试修改可能无法修改的记录的标识列。如果您不必更新记录的 id,则删除此语句

old.id = item.id;
于 2013-01-19T20:12:12.997 回答