Adding child via AddNew leaves IsNew = false

Adding child via AddNew leaves IsNew = false

Old forum URL: forums.lhotka.net/forums/t/4566.aspx


preludecrazy posted on Wednesday, March 26, 2008

Hi.

I've added the following to my LiteCompanyCollection:

protected override object AddNewCore()
{
LiteCompany
result;
result = LiteCompany.NewLiteCompany();
OnAddingNew(new AddingNewEventArgs(result));
Add(result);
return result;
}

And I'm adding a child to it like this:

LiteCompanyCollection objLCC = LiteCompanyCollection.GetLiteCompanyCollection(-1); //Get all companies.
LiteCompany objLC = objLCC.AddNew();
objLC.Name = "BBC";
objLC.URL = http://www.bbc.co.uk/;
objLCC.Save();

The new child is added fine, however when I save it, DataPortal_Update() thinks the object I added IsNew is false, so the Update method is called instead of the Insert method.  Am I supposed to be calling MarkNew() somewhere?

Thanks.

JoeFallon1 replied on Thursday, March 27, 2008

The last 2 lines of my DataPortal_Create are:

MarkNew()
ValidationRules.CheckRules()

So when you call .NewLiteCompany you should be using DataPortal_Create and it should be marking your child BO new.

I think the framework tries to do it for you automatically for root objects. I know Rocky moved the call to a couple of lines earlier in the code in 3.5 so I have to re-evaluate if I can now stop commenting it out. I was one of the few remaining coders who branched to DP_Create if the ID is not found in DP_Fetch and the BOs were getting mixed up with New and Old in earlier versions.

My templates have left the call to MarkNew and MarkOld even for root objects because I needed them set before calling check rules. I can re-evaluate them later too.

Joe

preludecrazy replied on Thursday, March 27, 2008

Thanks Joe.

ajj3085 replied on Thursday, March 27, 2008

What does your factory method look like?

Copyright (c) Marimer LLC