Hi David,
In my WCF Data Service, I am hoping to apply .NET Data Annotations on fields and have these annotations throw exceptions when they are violated. My plan is to catch exceptions in the following method and to prepare a more meaningful error message to return to the client.
protected override void HandleException(HandleExceptionArgs args)
{
//ORMEntityValidationException e;
//e.EntityValidated.
// Determine whether this exception if due to a PK or UKC violation.
if (args.Exception.GetType() == typeof(ORMQueryExecutionException))
{
}
}
For data annotation validation in Entity Framework, we have to do the following to get to the actual list of data validation errors, and to prepare a helpful error message for the client:
if (args.Exception.GetType() ==
typeof(DbEntityValidationException))
{
var ex = args.Exception as DbEntityValidationException;
var errors = ex.EntityValidationErrors.First().ValidationErrors.ToList();
var errorMessage = new StringBuilder();
foreach (System.Data.Entity.Validation.DbValidationError e in errors)
{
errorMessage.AppendLine(e.ErrorMessage);
}
args.Exception = new DataServiceException(500, errorMessage.ToString());
}
In LLBLGen, I've assigned a StringLength(4) data validation annotation to a string field. When I create a new record with a string that is longer than 4, I am not receiving any errors from LLBLGen or WCF Data Services. So, I am wondering whether I can use data annotations for validation in this manner with LLBLGen. If so, is there a similar list of validation error messages that I can process? If I cannot use data annotations in the WCF Data Service, then the alternative seems to be what you mentioned in your response. Right?
Thanks for your help and have a great weekend!
Mike