Error in Save()

Posts   
 
    
rod1978
User
Posts: 3
Joined: 01-Dec-2006
# Posted on: 01-Dec-2006 05:24:44   

I'm having this error when trying to save an entity.

"An exception was caught during the execution of an action query: Cannot insert the value NULL into column 'PartyID', table 'Funds.dbo.Telefono'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception."

Code:

PartyEntity party = new PartyEntity();

party = pAdministracionEntity.Party; trn.Add(party); trn.Add(party.Telefonos); trn.Add(party.Direcciones); trn.Add(party.ContactoElectronico); party.Save();

party.Telefonos.SaveMulti(); party.Direcciones.SaveMulti(); party.ContactoElectronico.Save();

trn.Commit();

the problem is that the partyEntity has an autonumeric Id, in the database the field is marked as autonumeric, i don't know why when debugging i make save "party" and looking at the watch the field PartyID is = 0, and then the party.Telefonos.SaveMulti() fails because it doesn't have a PartyId.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 01-Dec-2006 08:00:40   

Try the following code instead:


PartyEntity party = new PartyEntity();

party = pAdministracionEntity.Party;
trn.Add(party);
trn.Add(party.Telefonos);
trn.Add(party.Direcciones);
trn.Add(party.ContactoElectronico);
party.Save(true); // recursive save of party and all its related entities.
    
trn.Commit();

rod1978
User
Posts: 3
Joined: 01-Dec-2006
# Posted on: 01-Dec-2006 12:28:38   

Yes i have tryed that with the same results

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 01-Dec-2006 12:43:24   

do this:


PartyEntity party = null;
party = pAdministracionEntity.Party;
if(party.IsNew)
{
pAdministracionEntity.Party = new PartyEntity();
}
trn.Add(party);
trn.Add(party.Telefonos);
trn.Add(party.Direcciones);
trn.Add(party.ContactoElectronico);
party.Save();

The reason is that pAdministracionEntity.Party triggers lazy loading and returns a new entity if it's not found HOWEVER pAdministracion.Party isn't assigned to that new PartyEntity to prevent phantom inserts.

Frans Bouma | Lead developer LLBLGen Pro
rod1978
User
Posts: 3
Joined: 01-Dec-2006
# Posted on: 02-Dec-2006 22:42:22   

Thanks for the help, I realize that i was not loading values in the entity , sorry to bother you.