我有实体User与UserToUserCategories有一对多的关系。当我从数据库加载用户时,不要更改它而不是刷新会话,NHibernate将执行用户的UPDATE并增加它的版本。在我看来,这是一种不受欢迎的行为,想象一下,我加载了数百个用户,NHibernate会在刷新时更新它们。
public abstract class EntityBase
{
public virtual Guid Id { get; set; }
public virtual int Version { get; set; }
}
public class User : EntityBase
{
public virtual IList<UserToUserCategory> UserToUserCategories { get; set; }
}
public class EntityBaseMap<T> : ClassMap<T> where T : EntityBase
{
public EntityBaseMap()
{
this.OptimisticLock.Version();
this.DynamicUpdate();
this.Id(t => t.Id);
this.Version(t => t.Version);
}
}
public class UserMap : EntityBaseMap<User>
{
public UserMap()
{
this.HasMany(u => u.UserToUserCategories)
.NotFound.Ignore()
.Cascade.All()
.LazyLoad()
.AsBag()
.WithTableName("UserToUserCategory");
}
}
session = SessionSource.CreateSession();
var user = (from u in session.Linq<User>() select u).FirstOrDefault();
session.Flush(); // here NHibernate does UPDATE statement of user's version
这种映射不正确吗?我错过了什么?
答案 0 :(得分:3)