0

我通过添加创建了一个自定义规则

 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);
    }

但是现在我有了调用代码……</p>

try    
{    
    // some code
}
catch ( CodeSmith.Data.Rules… ??? )
{

// I can’t add the BrokenRuleException object. It’s not on the list.
}

我有:分配、安全和验证。

在 PLINQO 中捕获破坏规则异常的正确方法是什么?

4

1 回答 1

4

这是你需要做的,首先在你的项目中添加一个引用到

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; }

高温高压

于 2009-12-01T07:25:16.800 回答