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列を変更できなかったレコードのIDを変更しようとしています。レコードの ID を更新する必要がない場合は、このステートメントを削除してください

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