- 利用中斷點來找出驗證錯誤訊息
Step2:
Step3:
Step4:
Step5:
Step6:
- 透過 EntityValidationErrors 來顯示驗證錯誤訊息
try
{
uw.Save();
}
catch (DbEntityValidationException ex)
{
StringBuilder sb = new StringBuilder();
foreach (DbEntityValidationResult e in ex.EntityValidationErrors)
{
foreach (DbValidationError ve in e.ValidationErrors)
{
sb.AppendLine($"欄位 {ve.PropertyName} 發生錯誤: {ve.ErrorMessage}");
}
}
Console.WriteLine(sb.ToString());
}
- 自訂錯誤訊息
[Table("Employee")]
public partial class Employee
{
[Key]
public int EmployIeeD { get; set; }
[Required(ErrorMessage ="員工姓名為必填資訊")]
[StringLength(100)]
public string EmployeeName { get; set; }
}
1 則留言:
救了我一命 大感謝!
張貼留言