0

を追加してカスタムルールを作成しました

 static partial void AddSharedRules()
 {
            RuleManager.AddShared<Tag>(
                new CustomRule<String>(
                    "TagName",
                    "Invalid Tag Name, must be between 1 and 50 characters",
                    IsNullEmptyOrLarge));
 }

私のEntityクラスに。

次に、ルールを追加しました(ビデオには日付があり、間違った情報が含まれていますが、ビデオに表示されています)。

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

HTH

于 2009-12-01T07:25:16.800 に答える