2009-11-30 36 views
0

我已通過添加如何捕捉PLINQO中的BrokenRuleException?

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中捕獲違規規則異常的正確方法是什麼?

+0

太好了,現在我已經得到了價格是正確的主題曲停留在我的頭上。 – 2009-11-30 19:52:31

回答

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

+0

感謝Rippo,像往常一樣,您在這裏和@ PLINQO論壇都非常有幫助。 – 2009-12-01 12:00:32