SelfServicing Related Entity Quirk

Posts   
 
    
Trig
User
Posts: 96
Joined: 09-Jun-2004
# Posted on: 19-Dec-2005 16:55:44   

Frans, One of my developers is using SelfServicing on a project and has run into an interesting bug:

CaseEntity ce = new CaseEntity();
ce.FidelityCase.GroupId = int.Parse(this.txtGroupId.Text);  
ce.FidelityCase.Save();

FidelityCase is a 1:1 related entity. When he calls Save() on FidelityCase, the GroupId property is null, even though if we step into the set accessor for GroupId we can see the GroupId getting set. What appears to be happening is that the next time he references the FidelityCase property, the entity is refetched (which of course it can't be since its new) so a new instance is returned every time.

Is this by design, or a bug?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 19-Dec-2005 17:07:21   

That's by design. First set the related entity to a new entity, then set the properties, then save. You can change this behavior in the project properties and preferences of hte designer (project properties inherit from preferences when you create a project): LazyLoadingWithoutResultReturnsNew. Set that to false, regenerate code and you'll get null back when you trigger lazy loading.

Your code isn't correctly handling the situation in which an related entity isn't there. so either use the setting above or do:


CaseEntity ce = new CaseEntity();
FidelityEntity f = ce.FidelityCase;
if(f.Fields.State==EntityState.New)
{
f = new FidelityEntity();
ce.FidelityCase = f;
}
f.GroupId = int.Parse(this.txtGroupId.Text);
f.Save();

Frans Bouma | Lead developer LLBLGen Pro
Trig
User
Posts: 96
Joined: 09-Jun-2004
# Posted on: 19-Dec-2005 17:41:43   

Thanks Frans