我正在尝试更新名为“Hero”的表,正在使用DataContext
,代码如下所示:-
//linq to sql table (automatic generated)
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Hero")]
public partial class Hero : INotifyPropertyChanging, INotifyPropertyChanged
{
//fields methods etc..
}
还有我自己的偏职英雄...
public partial class Hero : IHero {
//my fields and methods etc...
public void Save() {
using(GameDBDataContext db = new GameDBDataContext()) {
db.Heros.Attach(this, true);
db.SubmitChanges();
}
}
}
但它正在抛出这个:-
System.InvalidOperationException: 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.
这个问题的解决方案是什么?
编辑:我试过这个: -
public void Save() {
using(GameDBDataContext db = new GameDBDataContext()) {
db.Heros.Attach(this, db.Heros.SingleOrDefault(x => x.id == EntityID));
}
}
它抛出:-System.NotSupportedException: 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.