我创建了一个自定义规则,方法是将
static partial void AddSharedRules()
{
RuleManager.AddShared<Tag>(
new CustomRule<String>(
"TagName",
"Invalid Tag Name, must be between 1 and 50 characters",
IsNullEmptyOrLarge));
}添加到我的实体类。
然后我添加了规则(就像在视频中看到的,尽管视频有日期并且有错误的信息):
public static bool IsNullEmptyOrLarge( string value )
{
return (value == null
|| String.IsNullOrEmpty(value)
|| value.Length > 50);
}但是现在我有了调用代码…
try
{
// some code
}
catch ( CodeSmith.Data.Rules… ??? )
{
// I can’t add the BrokenRuleException object. It’s not on the list.
}我有:赋值,安全和验证。
在PLINQO中捕获违规异常的正确方法是什么?
发布于 2009-12-01 15:25:17
下面是您需要做的,首先在您的项目中添加一个引用
System.ComponentModel.DataAnnotations
using CodeSmith.Data.Rules;然后
try
{
context.SubmitChanges();
}
catch (BrokenRuleException ex)
{
foreach (BrokenRule rule in ex.BrokenRules)
{
Response.Write("<br/>" + rule.Message);
}
}如果要更改默认消息,则可以转到实体并将属性从
[Required]至
[CodeSmith.Data.Audit.Audit]
private class Metadata
{
// Only Attributes in the class will be preserved.
public int NameId { get; set; }
[Required(ErrorMessage="please please please add a firstname!")]
public string FirstName { get; set; }您还可以使用这些类型的数据注记属性
[StringLength(10, ErrorMessage= "The name cannot exceed 10 characters long")]
[Range(10, 1000, ErrorMessage = "Value for {0} must be between {1} and {2}.")]
[RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Characters are not allowed.")]
public string FirstName { get; set; }HTH
https://stackoverflow.com/questions/1819526
复制相似问题