Save changes createing records with GUIDS

Posts   
 
    
JayWare
User
Posts: 14
Joined: 23-Sep-2010
# Posted on: 13-Oct-2010 00:37:41   

I'm not sure if this is LLBL Gen related, but I'm trying to do some changes to a collection and save them back - it's basically looping through, and then iterating through a table to see if the record find a match, if not, set to InActive and save.. I've tried doing parents.SaveMulti() at the end, reloading and resaving on each hit.. even just changing the field IsActive to false triggers a new record being added to the Parents table.. with guids in fields like Username..

I'm at a loss.. I cannot figure out how/why that bogus record is getting created/committed to the database.


IPredicateExpression filter = new PredicateExpression(PersonFields.IsParent == true);
            filter.AddWithAnd(PersonFields.CustomerGuid == custGuid);
            filter.AddWithAnd(PersonFields.IsActive == true);
            PersonCollection parents = new PersonCollection();

            parents.GetMulti(filter);
        
            foreach (PersonEntity parent in parents)
            {
                bool found = false;

                for (int i = 1; i < dtParents.Rows.Count; i++)
                {   
                    if ((parent.Firstname == dtParents.Rows[i][5].ToString() && parent.Lastname == dtParents.Rows[i][4].ToString()) || (parent.Email == dtParents.Rows[i][3].ToString() && parent.Email != ""))
                    {
                        found = true;
                    }               
                }

                if (!found)
                {

                    PersonEntity removeParent = new PersonEntity(parent.Guid);
                    removeParent.IsActive = false; <-- just this line alone will trigger a new record to be created in the db
                    removeParent.Save();
                }

            }

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 13-Oct-2010 04:27:36   

Try this:

 if (!found)
                {

                    parent.IsActive = false; <-- just this line alone will trigger a new record to be created in the db
                }
       }

       parents.SaveMulti();
                
David Elizondo | LLBLGen Support Team
JayWare
User
Posts: 14
Joined: 23-Sep-2010
# Posted on: 13-Oct-2010 15:55:12   

daelmo wrote:

Try this:

 if (!found)
                {

                    parent.IsActive = false; <-- just this line alone will trigger a new record to be created in the db
                }
       }

       parents.SaveMulti();
                

Thanks for the reply;

Saving does update the parent to inactive.. HOwever, it also creates a junk record. Even without saving - setting the parent.IsActive flag will result in a new "junk" record being added.

The file I'm importing is supposed set Jenings to Inactive.. and it does - but it addes the new record with Username = guid (see below)

JENINGS321 0 2009-08-20 17:43:45.683 SMITH321 1 2009-08-20 17:43:45.933 DAVIS321 1 2009-08-20 17:43:45.760 JONES321 1 2010-09-10 17:27:36.960 TRUMAN321 1 2009-08-20 17:43:45.793 eea6ce47-37a1-4d8e-883c-236295b242f4 1 2010-10-13 08:50:06.200 DOWNING321 1 2009-08-20 17:43:45.920 ANDREWS321 1 2009-08-20 17:43:45.717 FOX321 1 2009-08-20 17:43:45.840 HAWKINGS321 1 2009-08-20 17:43:45.857

Any ideas why the new junk "Person" record is getting created?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 13-Oct-2010 16:39:41   
                    PersonEntity removeParent = new PersonEntity(parent.Guid);
                    removeParent.IsActive = false; <-- just this line alone will trigger a new record to be created in the db
                    removeParent.Save();

There must be something in your code. It seemsthat either there is a database trigger, or an OnSave() event is handled that inserts the new record.

Please check removeParent.IsNew to see if a record has been fetched using LazyLoading after calling the CTor.

Irrelevant hint: when an item is Found, break from the inner loop.

JayWare
User
Posts: 14
Joined: 23-Sep-2010
# Posted on: 13-Oct-2010 20:01:21   

Walaa wrote:

                    PersonEntity removeParent = new PersonEntity(parent.Guid);
                    removeParent.IsActive = false; <-- just this line alone will trigger a new record to be created in the db
                    removeParent.Save();

There must be something in your code. It seemsthat either there is a database trigger, or an OnSave() event is handled that inserts the new record.

Please check removeParent.IsNew to see if a record has been fetched using LazyLoading after calling the CTor.

Irrelevant hint: when an item is Found, break from the inner loop.

Thanks for the tips.. but false alarm

I discovered another process in the code base that was generating new entities from reading the textfile - and it must be caching it, or something, and adding a new entity from a "blank" row.. which exposed the bug in the this code that wasn't checking for valid data before creating the entity.

With that cleared up, the LLBL gen entites SaveMulti(true) works slick to work on complex datasets.

Thanks,

Jay

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 14-Oct-2010 03:45:21   

JayWare wrote:

Thanks for the tips.. but false alarm

I discovered another process in the code base that was generating new entities from reading the textfile - and it must be caching it, or something, and adding a new entity from a "blank" row.. which exposed the bug in the this code that wasn't checking for valid data before creating the entity.

With that cleared up, the LLBL gen entites SaveMulti(true) works slick to work on complex datasets.

Thanks

Good you figured it out Jay wink

David Elizondo | LLBLGen Support Team