EntityValidator firing for unchanged entity

Posts   
 
    
Posts: 134
Joined: 04-Mar-2005
# Posted on: 09-Sep-2005 19:23:42   

I am trying to set FK of a new entity to an existing entity as follows:

                    currentProject = New MyProjectEntity(mainContract.ProjectId)
                    currentProject.IsNew = False

currentContract = New MyContractEntity(newId)
...
currentContract.project = currentProject
currentContract.save

The save attempts to save the project via the recursive save which, in turn, calls the project validator. The project validator detects that the project is missing a required field (which the underlying record has but the entity doesn't since it hasn't been fetched) and throws a validation error. If I remove the validation then the project doesn't get saved so I'm not sure why the validation is called. If there's no way to not call the validator is there a way to detect before validating that the entity is not going to be saved?

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 10-Sep-2005 02:32:00   

Are you running 1.2004.2. Also could you possibly paste more of the code for this problem? The behavior you're experiencing shouldn't happen.

Posts: 134
Joined: 04-Mar-2005
# Posted on: 10-Sep-2005 15:25:45   

bclubb wrote:

Are you running 1.2004.2. Also could you possibly paste more of the code for this problem? The behavior you're experiencing shouldn't happen.

Yes, I'm running the latest version (adapter over Oracle which I failed to mention before). The code that I have is scattered throughout various classes. What specifically did you want to check?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 11-Sep-2005 21:58:12   

In 1.0.2004.2, Validate is called in SaveEntity(), though after the check if the entity to save is actually dirty.

The order in which tasks happen is: - save dependent entities - save ourselves ----validate ----create query ----execute query ----if succeeded, refetch if required - save depending entities

So, the validation on project is called when project is saved. Could you check if this is actually true, if project is dirty/changed?

Frans Bouma | Lead developer LLBLGen Pro
Posts: 134
Joined: 04-Mar-2005
# Posted on: 11-Sep-2005 22:43:09   

Otis wrote:

So, the validation on project is called when project is saved. Could you check if this is actually true, if project is dirty/changed?

IsDirty is true because, I assume, the id field is set as part of creating the Project object. I assume that Ian had the same problem (http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=3139). I can manually set isDirty to False after creating the object but I'm a little baffled as to why the validator (or SaveEntity which calls the validator) thinks the object needs to be validated but not actually saved. confused

Note that I call the save on the contract, not the project, if that makes a difference?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 11-Sep-2005 23:45:27   

Ok, so a selfservicing save call, which isn't recursive. I was under the assumption you used adapter, so that might caused my confusion a little. Though a non-recursive save on a different entity would never end up doing anything with project indeed...

Frans Bouma | Lead developer LLBLGen Pro
Posts: 134
Joined: 04-Mar-2005
# Posted on: 12-Sep-2005 00:09:37   

Otis wrote:

Ok, so a selfservicing save call, which isn't recursive. I was under the assumption you used adapter, so that might caused my confusion a little. Though a non-recursive save on a different entity would never end up doing anything with project indeed...

I do use Adapter - sorry for the confusion - so the save is recursive. Saving the contract entity attempts to save the project as well. I only added the last point as additional information in case the method by which "needs validation" was determined is different for dependent entities for some reason.

Posts: 134
Joined: 04-Mar-2005
# Posted on: 15-Sep-2005 14:23:44   

<bump> smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 15-Sep-2005 14:56:54   

ChicagoKiwi wrote:

Otis wrote:

Ok, so a selfservicing save call, which isn't recursive. I was under the assumption you used adapter, so that might caused my confusion a little. Though a non-recursive save on a different entity would never end up doing anything with project indeed...

I do use Adapter - sorry for the confusion - so the save is recursive. Saving the contract entity attempts to save the project as well. I only added the last point as additional information in case the method by which "needs validation" was determined is different for dependent entities for some reason.

Ok, then this is the reason: Your code:


currentProject = New MyProjectEntity(mainContract.ProjectId) // A
currentProject.IsNew = False // B
currentContract = New MyContractEntity(newId)
currentContract.project = currentProject // C

On line A you define a new project entity. You set the PK field to ProjectId. This is now changed. On line B you set it to be a non-new entity, so this will have the effect of syncing currentProject.ProjectID with CurrentContract's FK.

As the PK of currentProject is set and changed, the project gets saved as it's assigned to currentContract and reachable from there.

Could you please verify if currentProject.Fields[(int)ProjectFieldIndex.ProjectId].IsChanged is true after B ?

(sorry for the late reply)

Frans Bouma | Lead developer LLBLGen Pro