Fluent Nhibernate problem

2020-04-30 02:44发布

问题:

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?