Persisting of a new object with "deleted" subobjects gives an exception.

Posts   
 
    
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 10-Nov-2005 16:29:48   

Hi I have the following problem. Consider the following code :


    //let the user select a node in the tree
    SeasonArticleEntity sa = node.Tag as SeasonArticleEntity;
    SeasonCarticleEntity sc =  sa.SeasonCarticle;
**  //sc.Save();
    sc.SeasonArticle.Remove(sa);
    sc.Save(true);
    //refresh the tree

Now this code works when sc is an existing entity in the database. But when sc "isNew" (and consequently sa is also new, because sa cannot exist in our business model without a parent sc) this code gives the following exception:


An exception was caught during the execution of an action query: Cannot insert the value NULL into column 'SEACAR_ID', table 'agro.dbo.SEASON_ARTICLE'; column does not allow nulls. INSERT fails.
The statement has been terminated.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception.

   at SD.LLBLGen.Pro.ORMSupportClasses.ActionQuery.Execute()
   at SD.LLBLGen.Pro.ORMSupportClasses.DaoBase.ExecuteActionQuery(IActionQuery queryToExecute, ITransaction containingTransaction)
   at Agro.Datalayer.DaoClasses.SeasonArticleDAO.AddSeasonArticle(IEntityFields fields, ITransaction containingTransaction)
   at Agro.Datalayer.EntityClasses.SeasonArticleEntityBase.InsertEntity()
   at SD.LLBLGen.Pro.ORMSupportClasses.EntityBase.Save(IPredicate updateRestriction, Boolean recurse)
   at Agro.Datalayer.EntityClasses.SeasonArticleEntityBase.Save(IPredicate updateRestriction, Boolean recurse)
   at SD.LLBLGen.Pro.ORMSupportClasses.EntityBase.Save(Boolean recurse)
   at SD.LLBLGen.Pro.ORMSupportClasses.EntityCollectionBase.SaveMulti(Boolean recurse)
   at Agro.Datalayer.CollectionClasses.SeasonArticleCollection.SaveMulti(Boolean recurse)
   at SD.LLBLGen.Pro.ORMSupportClasses.EntityBase.Save(IPredicate updateRestriction, Boolean recurse)
   at Agro.Datalayer.EntityClasses.ArticleEntityBase.Save(IPredicate updateRestriction, Boolean recurse)
   at SD.LLBLGen.Pro.ORMSupportClasses.EntityBase.Save(IPredicate updateRestriction, Boolean recurse)
   at Agro.Datalayer.EntityClasses.ArticleRelationEntityBase.Save(IPredicate updateRestriction, Boolean recurse)
   at SD.LLBLGen.Pro.ORMSupportClasses.EntityBase.Save(Boolean recurse)
   at SD.LLBLGen.Pro.ORMSupportClasses.EntityCollectionBase.SaveMulti(Boolean recurse)
   at Agro.Datalayer.CollectionClasses.ArticleRelationCollection.SaveMulti(Boolean recurse)
   at SD.LLBLGen.Pro.ORMSupportClasses.EntityBase.Save(IPredicate updateRestriction, Boolean recurse)
   at Agro.Datalayer.EntityClasses.ArticleEntityBase.Save(IPredicate updateRestriction, Boolean recurse)
   at SD.LLBLGen.Pro.ORMSupportClasses.EntityBase.Save(IPredicate updateRestriction, Boolean recurse)
   at Agro.Datalayer.EntityClasses.SeasonCarticleEntityBase.Save(IPredicate updateRestriction, Boolean recurse)
   at SD.LLBLGen.Pro.ORMSupportClasses.EntityBase.Save(Boolean recurse)
   at Agro.Detailforms.SeizoenForm.btnArtikelVerwijderen_Click(Object sender, EventArgs e)


Uncommenting the indicated line solves this problem, but i do not wish to save at this point. (because the transactional save really happens on a much later time)

Maybe i just didn't get enough sleep but it looks like pretty simple straightforward code, what am i missing ?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 10-Nov-2005 17:54:33   

change: sc.SeasonArticle.Remove(sa); into sa.SeasonCarticle = null;

it has the same effect, though it should work, as the sc.SeasonArticle.Remove triggers lazy loading.

You're still on 1.0.2004.2, so you've to deal with the new entities being returned from lazy loading, OR set the relatedEntityReturnsNewIfNotFound properties to false, so you'll get null instead of a new instance if it's not found due to lazy loading triggering. (this can be done in an include template for entity initialization).

Frans Bouma | Lead developer LLBLGen Pro