[英]WCF RIA Services Validation: Validate that Field is Unique Without Making it a Key
標題有點自我解釋。 我有一個已經定義了另一個鍵的實體(必須保持這種方式),並且我需要驗證某個字段是唯一的。
我希望使用在元數據中應用的Validator
。 但是我不能DomainService
那里使用DomainService
,因為代碼必須同時在服務器和客戶端上運行。 因此,我不知道如何訪問我的實體集以查看該字段的值是否唯一。
有想法嗎?
謝謝!
執行此操作的方法是在服務器端創建擴展名為.shared.cs
的文件。 將驗證器放在其中:
public class CustomValidation
{
public static ValidationResult ValidateUnique(object o, ValidationContext c)
{
#if SILVERLIGHT
//Do actual validation client-side:
MyObject mo = c.ObjectInstance as mo; //Object we are checking is type MyObject
MyDS ctx = new MyDS; //Domain Service
System.ServiceModel.DomainServices.Client.InvokeOperation<bool>
isUnique = //some invoke operation in Domain Service;
isUnique .Completed += (s, e) =>
{
if (!isUnique .HasError && !isUnique.Value)
{
//return error;
mo.ValidationErrors.Add(error);
}
};
#endif
//Server-side always returns success:
return ValidationResult.Success;
}
}
你可以這樣
字符串_Str = string.Empty; 公共字符串Str {獲取{return _Str; }
設置{if()//在此處添加您的代碼以檢查其是否唯一{
throw new Exception("Please Enter EmailId"); } _Str = value; OnPropertyChanged("Str"); }
這將是ur屬性的聲明,您可以使其唯一
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.