New Posts New Posts RSS Feed: DF2010-SL FK constraint validation
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

DF2010-SL FK constraint validation

 Post Reply Post Reply
Author
siko View Drop Down
Newbie
Newbie
Avatar

Joined: 26-Mar-2012
Posts: 27
Post Options Post Options   Quote siko Quote  Post ReplyReply Direct Link To This Post Topic: DF2010-SL FK constraint validation
    Posted: 03-Feb-2013 at 8:01am
Hi,
How can we:
- Change the
'The INSERT statement conflicted with the FOREIGN KEY constraint “FK_xxx. The conflict occurred in database “yyy’, table zzz, column www. The statement has been terminated.'
message into some more user readable
- set the more user friendly error info on the UI via INotifyDataErrorInfo?

Thanks.
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 03-Feb-2013 at 12:16pm
IMO you should only ever get such an error if you have a bug in your application. Your application should have validation logic to not even allow the user to save if it would result in a foreign key constraint error. That's the whole point of validation to ensure your entities are valid and have the necessary data integretity to be persisted to the database. Validation errors will be piped through to the UI via INotifyDataErrorInfo.
 
To learn more about how to implement validation logic, see the following links.
 
 
 
Back to Top
siko View Drop Down
Newbie
Newbie
Avatar

Joined: 26-Mar-2012
Posts: 27
Post Options Post Options   Quote siko Quote  Post ReplyReply Direct Link To This Post Posted: 04-Feb-2013 at 7:13am
I have found it difficult to understand where to place the validation logic in our solution, which follows the model/services/view model design like in the TempHire reference app.

How would I check for foreign key constraints?

My person entity has a location.

Person entity is exposed via a prop on the viewmodel and the list of locations are bound to a combobox which selected item is bound to the location property of the person entity?
How should I check that the location property on person has not been set?

A simple (cocktail) example would help a great deal!

Edited by siko - 04-Feb-2013 at 7:14am
Back to Top
siko View Drop Down
Newbie
Newbie
Avatar

Joined: 26-Mar-2012
Posts: 27
Post Options Post Options   Quote siko Quote  Post ReplyReply Direct Link To This Post Posted: 04-Feb-2013 at 7:35am
BTW I saw my previous post on this topic and the where question has been solved, now I remember it again :)

The question is that if I will simply throw a ValidationError exception, this will be shown via the I notifydataerrorinfo infra?

The example here shows that the service returns ok/not ok, but it's not clear to me how this error is communicated further on...

Edited by siko - 04-Feb-2013 at 7:36am
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 04-Feb-2013 at 7:41am
You can actually find this very example in TempHire.

https://github.com/IdeaBlade/Cocktail/blob/master/Samples/TempHire/DomainModel/Address.cs

See the Validate method at the bottom. The method is called by the EntityManagerDelegate during a save and if validation errors occur, Cocktail automatically cancels the save and notifies the UI as documented (see above link).

https://github.com/IdeaBlade/Cocktail/blob/master/Samples/TempHire/TempHire/EntityManagerDelegate.cs

You can't force this particular validation in TempHire at the moment, because the way the current combobox is bound, the State property is always set to a valid state. You can see other validation rules in action, though by not filling in the zipcode for example. The validation rules for the zip code are specified using validation attributes on the property. Cocktail automatically executes all validation rules before allowing the save to proceed. 

[DataMember]
[Required]
[StringLength(10, MinimumLength = 5)]
        public string Zipcode { get; set; }

Besides the red boxes on the UI in case of a validation error, TempHire also reacts to validation errors by displaying an error popup. It does this by implementing IValidationErrorNotification, which in turn simply publishes a message to the EventAggregator.

https://github.com/IdeaBlade/Cocktail/blob/master/Samples/TempHire/Common/Validation/ValidationErrorHandler.cs

The message is then processed by the ValidationErrorMessageProcessor, which displays the error popup.

https://github.com/IdeaBlade/Cocktail/blob/master/Samples/TempHire/Common/Validation/ValidationErrorMessageProcessor.cs

Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 04-Feb-2013 at 7:51am
I just realized that you can actually force the State validation. Click Add, enter first/last name in the popup, click ok and then immediately hit Save without entering anything else. You'll see the error popup I described above with all the validation errors including the missing State. It doesn't currently display the red box around the drop down, though, because the styling is wrong. 
Back to Top
siko View Drop Down
Newbie
Newbie
Avatar

Joined: 26-Mar-2012
Posts: 27
Post Options Post Options   Quote siko Quote  Post ReplyReply Direct Link To This Post Posted: 06-Feb-2013 at 10:23am
Thanks for the elaborate post.
 
I was able to do a similar thing.
We are code-second, so I introduced a buddy class for the auto generated entity class and had it implement IValidate with one method: Validate(VerifierResultCollection validationErrors)
With this in mind and the binding being in a valid state, I checked if the _id properties = 0, and then followed your example.
Validation worked as advertised and we found some problems with our style/controls too :)
 
Again, many thanks.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down