2011-11-30 99 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()什麼的。

相關問題