Bridget the Midget
Bridget the Midget

Reputation: 201

Make NHibernate overwrite data in collection

Lets say I have a class:

public class Entity
{
  ...
  public IEnumerable<Cousin> Cousins { get; set; }
}

... and I've mapped it...

public class EntityMap : ClassMap<Entity>
{
  public EntityMap()
  {
    ...
    HasMany(x => x.Cousins).Cascade.AllDeleteOrphan();
  }
}

Cousin does not have a repository of its own (at least not for now). I wan't to do all savings through Entity and its repository.

I have already stored a list of cousins, so the Entity has items in Cousins.

The next time I want to save Entity, it might have new Cousins. Therefore I want the existing Cousins to disappear and make room for the new collection of Cousins.

I'm trying to do this but I encounter this error message:

A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: Cousins

How can I make NHibernate overwrite Cousins everytime I save Entity? Also, if this is bad practice - say so.

Upvotes: 1

Views: 796

Answers (1)

jorgehmv
jorgehmv

Reputation: 3713

It looks like you are changing the reference of the collection, i mean making something like x.Cousings = new List<Cousin>()

you should do x.Clear() and then add each element with x.Add(someCousin)

Upvotes: 3

Related Questions