开发者

NHibernate bag is always null

开发者 https://www.devze.com 2022-12-31 20:07 出处:网络
I have set up my mapping file and classes as suggested by many articles class A { ... IList BBag {get;set;} ... }

I have set up my mapping file and classes as suggested by many articles

class A { ... IList BBag {get;set;} ... }

class B { ... A aObject {get;set;} ... }

<class name="A">...<bag na开发者_运维知识库me="BBag" table="B" inverse="true" lazy="false"><key column="A_ID" /><one-to-many class="B" /></bag>...

<class name="B">...<many-to-one name="aObject" class="A" column="A_ID" />...

I added a set of A's to the A table and a set of B's to the B table, all the data is stored as expected. However if I try and access aInstance.BBag.Count I get a null reference exception. I think I missing some key knowledge on how an bag gets instantiated. Thanks


When you call Save()/SaveOrUpdate()/Update() the collections of the saved object will not be instantiated/hydrated.

As such you have to select the object again if you want the collection elements. If you don't want to do that you can make a collection of the saved collection objects and assign them to the saved object's collection property.


I did ARepository.Update(BOject); and when I called it back it worked for some reason I thought the update would happen automatically. Is there a way to do automatically update a bag, i.e. some mapping file setting?

0

精彩评论

暂无评论...
验证码 换一张
取 消