ASP.NET, LLBLGen 2.0.7.111
The CustomerId is a foreign key to another table and we just want to make sure that ReferenceNumber is not duplicated by Customer.
So, I have a unique constraint on those two fields - CustomerId and ReferenceNumber.
All works fine.
Now, when the customer is inputting data and hits the save button, I want to verify this is unique BEFORE attempting to save so I can give a nice error message. I had hoped to do this in the InsertEntity() and UpdateEntity() overload.
I put the folllowing code in place. This causes Save() to return false if it fails for this reason and the entity will be loaded with the duplicate one. Works fine.
protected override bool InsertEntity()
{
RequestEntity requestEntity = new RequestEntity();
requestEntity.FetchUsingUCReferenceNumberCustomerId(this.ReferenceNumber, this.CustomerId);
if (!requestEntity.IsNew) //we found it...
{
////TODO have to refetch??? There should be a better way!
this.FetchUsingUCReferenceNumberCustomerId(this.ReferenceNumber, this.CustomerId);
return false; //do not save anything!
}
return base.InsertEntity();
}
Now, when they updated a record, I needed to make sure they did not change the ReferenceNumber to one that already existed, so I put similar code in for that.
Problem is there is specific code on the support classes that cause an exception to be thrown if the Save() returns false AND it is an update! If it is an insert, the exception is not thrown.
What are the suggestions on how to implement the above behavior. I was planning on implementing this in the DataLayer - I want it to fail the save, but fail politely.