如何解决UpdateModel + NHibernateException“ xxx实例的标识符从x更改为y”
| 我正在将ASP.NET MVC与NHibernate一起使用public class User
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Country Country { get; set; }
}
public class Country
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
我有一个页面可以编辑用户,
有一个下拉列表选择国家
我正在使用UpdateModel()更新用户。
它对User.Name正常工作。
它更新User.Country.Id,但不更新User.Country.Name
(因为该页面仅发送Country.Id)。所以我在用这个:User.Country = countryDao.Get(User.Country.Id)
但是当我保存用户时,我有一个NHibernateException:{\“ Country实例的标识符从1更改为2 \”}
我能做什么 ?
解决方法
错误:
var db = new BaseServices<Supplier>();
Supplier item = db.Get(model.SupplierID);
**TryUpdateModel(item);
item.Locality = new BaseServices<Locality>().Get(model.Locality.LocalityID);**
db.Update(item);
db.SaveChange();
溶出度:
var db = new BaseServices<Supplier>();
Supplier item = db.Get(model.SupplierID);
item.Locality = new BaseServices<Locality>().Get(model.Locality.LocalityID);
TryUpdateModel(item);
db.Update(item);
db.SaveChange();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。