IEntity.Save() return true but no record in database

Posts   
 
    
gabrielk avatar
gabrielk
User
Posts: 231
Joined: 01-Feb-2005
# Posted on: 19-Sep-2007 08:15:48   

Hi All,

I'm building a generic ASP.net CheckBox list which populates based on a EntityCollectionName and saves the selected/deselected items in a linktable (i forgot the proper term), for example like:

Product - ProductFeature - Feature

The CheckBoxList.PopulateWithEntity would be FeatureEntity The CheckBoxList.LinkCollection would be ProductFeatureCollection The CheckBoxList.LinkEntityName would be ProductFeatureEntity

(This is just my first initial draft on this, I can see how it can be made cleaner, but I prefer first to work on a proof-of-concept and later refine)

The following code is performed on SelectedIndexChanged:


            // Loop through all items of the checkboxlist, their values represent the primary key of the Collection that is bound to this CbList
            for (int i = 0; i < this.Items.Count; i++)
            {
                System.Web.UI.WebControls.ListItem li = this.Items[i];
                // Get the primary id
                int selectedItemId = Convert.ToInt32(li.Value);

                // Dynamicly retrieve the LinkTable collection from as a property from the DataSource of the Page
                // This means we retrieve the collection already filtered for the DataSource
                SD.LLBLGen.Pro.ORMSupportClasses.IEntityCollection itemLinkCollection = Dionysos.Reflection.Member.InvokeProperty(this.PageAsLLBLGenEntity.DataSource,this.linkCollectionOnParentDataSource) as SD.LLBLGen.Pro.ORMSupportClasses.IEntityCollection;

                // Try to find the Entity in the retrieved collection, if not found return null
                SD.LLBLGen.Pro.ORMSupportClasses.IEntity item = this.FindEntity(itemLinkCollection, selectedItemId);
                if (li.Selected)
                {
                    // Add if not found 
                    if (item == null)
                    {
                        // Get the name of the PK field for the Collection bound to this CbList
                        string primaryKeyFieldOfBoundEntity = this.EntityName + "Id"; 
                        // Get the name of the PK field of the DataSource of the page
                        string primaryKeyFieldOfPageEntity = this.PageAsLLBLGenEntity.DataSource.PrimaryKeyFields[0].Name;
                        // Dynamicly create an entity based on the LinkEntityName
                        item = DataFactory.EntityFactory.GetEntity(this.LinkEntityName) as SD.LLBLGen.Pro.ORMSupportClasses.IEntity;
                        // Populate it's fields 
                        item.Fields[primaryKeyFieldOfBoundEntity].CurrentValue = selectedItemId;
                        item.Fields[primaryKeyFieldOfPageEntity].CurrentValue = this.PageAsLLBLGenEntity.EntityId;
                        // Save
                        item.Save();
                    }
                }
                else
                {
                    // If found but not selected removed the Entity.
                    // Here no problems, .Delete() works.
                    if(item!=null)
                        item.Delete();
                }
            }

The strange thing is everything works, but the Entity doesn't actually get saved. The item.Save() returns a true, but it's not actually put in the persistened storage. What am I overlooking?

Before I save in the Immidiate the following values are returned: item returns:


{ExtraVestiging.Data.EntityClasses.ProductMeasurementEntity}
    [ExtraVestiging.Data.EntityClasses.ProductMeasurementEntity]: {ExtraVestiging.Data.EntityClasses.ProductMeasurementEntity}
    Fields: {SD.LLBLGen.Pro.ORMSupportClasses.EntityFields}
    IsSerializing: false
    PrimaryKeyFields: Count = 1

primaryKeyFieldOfBoundEntity returns: "MeasurementId"

primaryKeyFieldOfPageEntity returns: "ProductId"

item.Fields[primaryKeyFieldOfBoundEntity].CurrentValue returns: 2 (which is correct)

item.Fields[primaryKeyFieldOfPageEntity].CurrentValue returns: 38 (which is correct)

item.Save() returns:

But when I look in the ProductMeasurement table, even in debug directly after the .Save line (to make sure nothing else deletes it afterwards) it's not there... Btw: If I manually add an entity to the linkcollection via the SQL Man. Studio and uncheck the checkbox the item.Delete() works.

Also after the save:

item.Fields["ProductMeasurementId"].CurrentValue returns null

And

item.Fields["ProductMeasurementId"] returns: {SD.LLBLGen.Pro.ORMSupportClasses.EntityField} [SD.LLBLGen.Pro.ORMSupportClasses.EntityField]: {SD.LLBLGen.Pro.ORMSupportClasses.EntityField}

Any suggestions which probably big thing I am missing?

Thanks, Gab

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 19-Sep-2007 11:41:39   
                    // Populate it's fields 
                    item.Fields[primaryKeyFieldOfBoundEntity].CurrentValue = selectedItemId;
                    item.Fields[primaryKeyFieldOfPageEntity].CurrentValue = this.PageAsLLBLGenEntity.EntityId;
                    // Save
                    item.Save();

Directly setting the CurrentValue won't flag the field as changed and won't flag the entity as being dirty. Instead use SetNewFieldValue() method.

gabrielk avatar
gabrielk
User
Posts: 231
Joined: 01-Feb-2005
# Posted on: 19-Sep-2007 12:41:28   

Thanks!

Cheers, Gab