为什么NHibernate更新引用实体?

为什么NHibernate更新引用实体?,nhibernate,Nhibernate,我有一个测试方法如下: public void Add_Update_Delete_a_Registration() { ISessionFactory sessionFactory = SessionFactory.GetSessionFactory(connString); using (ISession session = sessionFactory.OpenSession()) { Course course =

我有一个测试方法如下:

       public void Add_Update_Delete_a_Registration() {

        ISessionFactory sessionFactory = SessionFactory.GetSessionFactory(connString);
        using (ISession session = sessionFactory.OpenSession()) {

            Course course = new CourseRepository(session).GetById(12019);

            Registration entity = new Registration();
            entity.Course = course; //Assign the Course to register

            //assign other entity members
            //...

            RegistrationRepository repository = new RegistrationRepository(session);
            repository.Add(entity);
        }
已正确插入注册实体

问题是,NHibernate还进行了更新数据库调用,以更新在测试方法中根本没有更改的课程实体。可能的原因是什么

映射:

    public class CourseMap : ClassMap<Course>{
    public CourseMap() {
        Id(x => x.Id).GeneratedBy.HiLo("100");
        Map(x => x.WeekDay)
            .Not.Nullable()
            .CustomType<int>(); //WeekDay is type of DayOfWeek enums
        References(x => x.Room)
            .Not.Nullable();
        Map(x => x.StartTime)
            .Not.Nullable();
        Map(x => x.EndTime)
            .Not.Nullable();
        Map(x => x.CreatedTime)
            .Not.Nullable();
        Map(x => x.UpdatedTime);
        Map(x => x.CreatedBy)
            .Not.Nullable();
        Map(x => x.UpdatedBy);
        Version(x => x.Version).Column("RCB_Version")
            .CustomSqlType("timestamp")
            .Generated.Always()
            .Not.Nullable();
    }

    public class RegistrationMap : ClassMap<Registration>{
    public RegistrationMap() {
        Id(x => x.Id)
            .GeneratedBy.HiLo("100");
        Map(x => x.OwnerWindowsAccount)
            .Not.Nullable()
            .Length(50);
        References(x => x.Course)
            .Not.Nullable();
        Map(x => x.TrainingDate)
            .Not.Nullable();
        Map(x => x.RegistreeName)
            .Not.Nullable()
            .Length(50);
        Map(x => x.RegistreeWindowsAccount)
            .Nullable()
            .Length(50);
        Map(x => x.CreatedTime)
            .Not.Nullable();
        Map(x => x.UpdatedTime);
        Map(x => x.CreatedBy)
            .Not.Nullable();
        Map(x => x.UpdatedBy);
        Version(x => x.Version)
            .CustomSqlType("timestamp")
            .Generated.Always()
            .Not.Nullable();
    }
}
公共类课程地图:类地图{
公共课程地图(){
Id(x=>x.Id).GeneratedBy.HiLo(“100”);
地图(x=>x.WeekDay)
.Not.Nullable()
.CustomType();//WeekDay是DayOfWeek枚举的类型
参考(x=>x.Room)
.Not.Nullable();
映射(x=>x.StartTime)
.Not.Nullable();
映射(x=>x.EndTime)
.Not.Nullable();
映射(x=>x.CreatedTime)
.Not.Nullable();
映射(x=>x.updateTime);
映射(x=>x.CreatedBy)
.Not.Nullable();
Map(x=>x.UpdatedBy);
版本(x=>x.Version).列(“RCB_版本”)
.CustomSqlType(“时间戳”)
.Generated.Always()
.Not.Nullable();
}
公共类注册映射:类映射{
公共注册地图(){
Id(x=>x.Id)
.由.HiLo生成(“100”);
映射(x=>x.ownerWindows帐户)
.Not.Nullable()
.长度(50);
参考文献(x=>x.Course)
.Not.Nullable();
地图(x=>x.TrainingDate)
.Not.Nullable();
映射(x=>x.RegistreeName)
.Not.Nullable()
.长度(50);
映射(x=>x.RegistreeWindowsAccount)
.Nullable()
.长度(50);
映射(x=>x.CreatedTime)
.Not.Nullable();
映射(x=>x.updateTime);
映射(x=>x.CreatedBy)
.Not.Nullable();
Map(x=>x.UpdatedBy);
版本(x=>x.Version)
.CustomSqlType(“时间戳”)
.Generated.Always()
.Not.Nullable();
}
}
非常感谢!
Leo

有一些不同的问题可能会导致这种情况。我经常在映射错误的枚举或以错误的方式使用reverse=“true | false”时遇到这种情况。

您指定了版本列。这意味着任何属性更改(甚至集合)都会触发版本更新

为了防止某个属性/集合更改版本,应在xml映射中设置
optimistic lock=“false”
属性


虽然不确定它的语法是否流畅。可能是.OptimisticLock.False()或其他什么。

我在课程映射中有一个枚举属性,即DayOfWeek。映射是Map(x=>x.WeekDay)。不是.Nullable().CustomType();。但是我没有任何使用反向的映射。我将在我的原始帖子中添加映射。谢谢。