PK Not Changing on Save

Posts   
 
    
polarbear
User
Posts: 5
Joined: 23-Oct-2006
# Posted on: 02-Nov-2006 03:18:24   

LLBLGen Pro v. 1.0.2005.1

Hi guys,

I have a problem, I hope somebody can help.

I have a simple entity object:

ProductEntity

ProductId - PK ProductDescription

ProductId is PK but it is not an identity. So I have a form that fills out the info for the ProductEntity. Since PK is not an identity, there may be cases when the user enters a PK that already exists, in which case we get some PRIMARY KEY violation exception. The strange thing is that when this happens, no matter what value I change the ProductId to afterwards (upon pressing BACK on the web brower OR re-entering the webform), and then save, I ALWAYS get that same PRIMARY KEY violation exception. I am creating that ProductEntity the moment before I save it, so there should not be any residual state on that Entity. In fact, I put a break point right up to the point before calling Save(), and the ProductId value is indeed correct (ie. it is one that does not exist), but then when I Save, i still get that exception. I checked the ORMException whatever exception, and find that even though the ProductId for the Entity is the new value, the generated SQL Statement has the ORIGINAL ProductId i first entered that generated the PRIMARY KEY violation. I don't know how is this possible, as I said, I create a NEW ProductEntity and populate the values upon the user pressing Save. Any help?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 02-Nov-2006 06:55:28   
polarbear
User
Posts: 5
Joined: 23-Oct-2006
# Posted on: 02-Nov-2006 13:39:58   

i don't think it's anything unusual.. but here's a snippet of the relevant code

void _btnSave_Click(object sender, System.EventArgs e) { ProductEntity productEntity = new ProductEntity(); ApplyControlValues( productEntity ); CreateProduct( productEntity ); }

void ApplyControlValues( ProductEntity productEntity ) { productEntity.ProductId = Convert.ToInt32( _txtProductId.Text ); productEntity.ProductDescription = _txtProductDescription.Text; }

void CreateProduct( ProductEntity productEntity ) { using ( DataAccessAdapter adapter = GetDataAccessAdapter() ) { adapter.SaveEntity( productEntity ); } }

i really don't understand WHY the initial old values are maintained. i think it's a clue that it only occurs AFTER i trigger the PRIMARY KEY violation once... THEN it behaves this way -- that no matter what value i enter, it will always have the same original ProductId and hence the violation... as if somehow it's being cached somewhere. but as you can see, i'm 'new'ing every ProductEntity on save AND i can see the value is indeed correct right before the SaveEntity call (but it doesn't appear so AFTER the call). if i don't trigger that primary key violation.. everything is normal. Thanks for the help.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 02-Nov-2006 14:27:13   

That's very strange disappointed

Could you please post the exact exception text and the stack trace?

Also please make sure that you are comparing PKs in the database that is being targeted by the code. (Catalog name in the LLBLGen Pro Designer) As this can not be changed by the connection string. It needs some Catalog Names overrites.

Another good idea is to examine the generated SQL query and try to run it directly against the database. Refer to LLBLGen Pro manual: "Using the generated code -> Troubleshooting and debugging"