updating an existing entity

Posts   
 
    
tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 04-Aug-2005 01:00:12   

Can anyone tell me why this code does not work? I'm trying to create an instance of the ProductVersion entity for update that I pass to the detailEntity instance by setting the relation with SetRelatedEntity. This always fails because I think the SetRelatedEntity is not working the way I expect.


                int productVersionID = (int)e.Row.DataKeyValue;

                ProductVersionEntity productVersion = new ProductVersionEntity(productVersionID);
                productVersion.IsNew = false;

                EntityBase2 detailEntity = ServiceFactory.GetProductManager().CreatePartDetail(productType);
                detailEntity.SetRelatedEntity(productVersion, "ProductVersion");
                detailEntity.SetNewFieldValue("ProductVersionID", productVersionID);
                detailEntity.IsNew = false;

                foreach(GridEXCell cell in e.Row.Cells) 
                {
                    if ( cell.DataChanged )
                        detailEntity.SetNewFieldValue( cell.Column.DataMember, cell.Value );
                }
                ServiceFactory.GetPersistanceManager().SaveEntity(detailEntity);

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 04-Aug-2005 09:23:05   

                detailEntity.SetRelatedEntity(productVersion, "ProductVersion");
                detailEntity.SetNewFieldValue("ProductVersionID", productVersionID);

Why not: detailEntity.ProductVersion = productVersion; ?

that will set ProductVersionID as well.

You can step into SetRelatedEntity to see if it does its work, it's a generated method. Does it set the related entity?

Frans Bouma | Lead developer LLBLGen Pro
tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 10-Aug-2005 02:27:08   

Otis wrote:


                detailEntity.SetRelatedEntity(productVersion, "ProductVersion");
                detailEntity.SetNewFieldValue("ProductVersionID", productVersionID);

Why not: detailEntity.ProductVersion = productVersion; ?

that will set ProductVersionID as well.

You can step into SetRelatedEntity to see if it does its work, it's a generated method. Does it set the related entity?

The reason that I can't use this that detailEntity is an EntityBase2 instance and so the property ProductVersion doesn't exist. I can't cast it either because the type is not known at run time. Here is the code that I have working for this, but I thought there might be a more efficient way of doing this.


int productVersionID = (int)e.Row.DataKeyValue;
ProductVersionEntity productVersion = new ProductVersionEntity();
productVersion.ProductVersionID = productVersionID;
productVersion.IsNew = false;

EntityBase2 detailEntity = ServiceFactory.GetProductManager().CreatePartDetail(productType);
detailEntity.IsNew = false;
detailEntity.SetNewFieldValue("ProductVersionID", productVersionID);
detailEntity.GetType().GetProperty("ProductVersion", typeof(ProductVersionEntity)).SetValue(detailEntity, productVersion, new object[0]);

foreach(GridEXCell cell in e.Row.Cells) 
{
    if ( cell.DataChanged )
        SetNewFieldValue(detailEntity, cell.Column.DataMember, cell.Value);
}
ServiceFactory.GetPersistanceManager().SaveEntity(detailEntity);

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 10-Aug-2005 10:17:59   

Ok, but you had these 2 lines:


                detailEntity.SetRelatedEntity(productVersion, "ProductVersion");
                detailEntity.SetNewFieldValue("ProductVersionID", productVersionID);

the first line sets the related entity. If productVersion.ProductVersionID is different from productVersionID, it will reset the reference in the second line, if they're the same, it will be a no-op. In your second posting, you show that the value is indeed the same, so by using detailEntity.SetRelatedEntity() you already set the related entity AND because productVersion is not new, it will sync the FK in detailEntity with the PK of productVersion as well, effectively setting detailEntity.ProductVersionID.

Saving detailEntity should then result in an updated detailEntity in the db. So with the single line:


detailEntity.SetRelatedEntity(productVersion, "ProductVersion");

you should have what you want. If you step over that line, detailEntity.ProductVersionID should be equal to the value of productVersionID, is that correct?

Frans Bouma | Lead developer LLBLGen Pro
tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 11-Aug-2005 00:19:26   

Thanks Otis, this worked.