Fluent Nhibernate problem

坚强是说给别人听的谎言 提交于 2019-12-31 04:58:07

问题


I have this in my entity:

public virtual Iesi.Collections.Generic.ISet<long> Blas { get; set; }

and this for my mapping:

mapping.HasMany(x => x.Blas).AsSet().Element("Value", m => m.Type<long>()); 

This creates the relevant tables and I add data like this:

X.Blas = new Iesi.Collections.Generic.HashedSet<long>();
X.Blas.Add(some_long);

This adds values to the object but the values in Blas are never persisted (everything else of X is).

Can anyone see anything wrong?

Thanks.

Christian


回答1:


if X is loaded through a session then blas is initialized with a changetracking collection. So dont overwrite it. Try X.Blas.Clear(); instead of X.Blas = new Iesi.Collections.Generic.HashedSet<long>();




回答2:


Try adding a cascade setting.

mapping.HasMany(x => x.Blas).AsSet()
       .Element("Value", m => m.Type<long>())
       .Cascade.AllDeleteOrphan();

Also you should just be able to use ICollection and a regular Hashset instead of Iesi. Provided you're using at least version 3 (it might work with 2.1.2 or higher as well)




回答3:


You should follow proper object oriented encapsulation to avoid problems like this, an example in my post here: How do I map a collection accessed through a read only property?



来源:https://stackoverflow.com/questions/7109438/fluent-nhibernate-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!