2011-11-30 97 views
2

我有一个测试评判象下面这样:为什么NHibernate更新参考实体?

 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也做了UPDATE数据库调用来更新课程实体,它在测试方法中根本没有改变。可能的原因是什么?

的映射:

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(); 
    } 
} 

非常感谢! 狮子座

回答

0

有一些可能导致此问题的各种问题。当我将我的枚举映射为错误的或者正确地使用inverse =“true | false”时,我经常会得到它。

+0

我在课程映射中有一个枚举属性,即DayOfWeek。映射是Map(x => x.WeekDay).Not.Nullable()。CustomType ();.但是我没有使用逆的映射。我将在原始文章中添加映射。谢谢。 – user538220

0

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

为了防止某个属性/集合改变版本,应该在xml映射中设置optimistic-lock="false"属性。

虽然不知道它将如何在流利的语法。可能是.OptimisticLock.False()什么的。

相关问题