我已经引用了与同一问题相关的所有现有线程,但没有找到任何帮助。
基本上,我正在创建一个包含一些验证错误的EntityError集合。我将EntityErrors添加到此集合中,如下所示:
if (!String.IsNullOrEmpty(user.UserCode))
{
if (_usersRepo.HasUserCode(user.Id, user.UserCode, clientId))
{
entityErrors.Add(new EntityError() { EntityTypeName = entityTypeName, ErrorMessage = "UserCode already exist", ErrorName = errorName, PropertyName = "UserCode", KeyValues = new object[] { user.UserCode } });
}
}
然后我按如下方式抛出集合:
throw new EntityErrorsException(validationErrors);
只有在将KeyValues引入EntityError对象之后才会出现上述错误,而不会将错误抛出到应用程序并正确显示。
正如以前的一些帖子所建议的那样,我在调用此验证之前尝试添加Manager.FetchMetaData
,但它仍无效。
以下是我收到错误时的http响应与其按预期工作时的响应。你可以看到,当抛出错误时,KeyValues有一个值,当它工作时KeyValues为null。
什么时候有效:
{"$id":"1","$type":"Breeze.ContextProvider.SaveError, Breeze.ContextProvider","Message":"Exception of type 'Breeze.ContextProvider.EntityErrorsException' was thrown.","EntityErrors":[{"$id":"2","$type":"Breeze.ContextProvider.EntityError, Breeze.ContextProvider","ErrorName":"Attribute Validation Error","EntityTypeName":"Users","KeyValues":null,"PropertyName":"UserCode","ErrorMessage":"UserCode already exist"}]}
enter code here
错误时:
{"$id":"1","$type":"Breeze.ContextProvider.SaveError, Breeze.ContextProvider","Message":"Exception of type 'Breeze.ContextProvider.EntityErrorsException' was thrown.","EntityErrors":[{"$id":"2","$type":"Breeze.ContextProvider.EntityError, Breeze.ContextProvider","ErrorName":"Attribute Validation Error","EntityTypeName":"Users","KeyValues":["782"],"PropertyName":"UserCode","ErrorMessage":"UserCode already exist"}]}