public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { var db = new 客戶資料Entities(); if (this.Id == 0) { if (db.客戶聯絡人.Where(s => s.客戶Id == this.客戶Id && s.Email == this.Email).Any()) { yield return(new ValidationResult("Email 已存在", new string[] { "Email" })); } } else { if (db.客戶聯絡人.Where(s => s.Id != this.Id && s.客戶Id == this.客戶Id && s.Email == this.Email).Any()) { yield return(new ValidationResult("Email 已存在", new string[] { "Email" })); } } yield return(ValidationResult.Success); }
public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { 客戶資料Entities db = new 客戶資料Entities(); var 客戶聯絡人 = db.客戶聯絡人.AsQueryable(); bool checkExistingEmail = false; if (this.Id == 0) { checkExistingEmail = 客戶聯絡人.Any(x => x.是否已刪除 == false && x.客戶Id == 客戶Id && x.Email == Email); } else { checkExistingEmail = 客戶聯絡人.Any(x => x.是否已刪除 == false && x.客戶Id == 客戶Id && x.Email == Email && x.Id != this.Id); } if (checkExistingEmail) { yield return(new ValidationResult("已經存在相同的 Email", new string[] { "Email" })); //一個label只能有一個錯誤訊息 } yield break; }