On Wed, Jun 9, 2010 at 5:04 PM, Oskar Berggren <[email protected]>wrote:
> 2010/6/9 Eddie <[email protected]>: > > Hi all. > > > > I'm trying to manage collections. I get an error when I use the > > annotation "[Transaction]" in my method. > > The error is > > "A collection with cascade="all-delete-orphan" was no longer > > referenced by the owning entity instance: Cuenta" > > > > My method > > > > [CODE] [Transaction] > > public void AgregarCuenta(int idPersona, string cuenta) > > { > > Persona persona = RepositoryPersona.Get(idPersona); > > > > IList<Cuenta> cuentas = new List<Cuenta>(); > > Cuenta cta = new Cuenta(); > > cta.Numero = cuenta; > > cta.Saldo = 100; > > cta.Medico = persona; > > cuentas.Add(cta); > > > > persona.Cuentas.Clear(); > > persona.Cuentas = cuentas; > > I would normally try to make the Cuentas setter private. Then instead do: > foreach (var c in cuentas) > persona.Cuentas.Add(c); // or persona.AddCuenta(c); > > (Or in this case, skip the local cuentas and add directly to > personas.Cuentas.) > > This way the persona is in charge of the collection it owns. > > In addition to Oskar's guidance, and like the error message says, you can't just remove/overwrite the collection that HNibernate put into the Persona class. You must just use that collection and perform any additions/removals on it. the last line of your code is a "no-no". -- thanks cliff -- You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
