2013-05-17 43 views
8

tôi nhận được lỗi này:Xác thực không thành công cho một hoặc nhiều pháp nhân. Xem thuộc tính 'EntityValidationErrors' để biết thêm chi tiết. Mã Đầu tiên

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. 

khi tôi cố gắng cập nhật cơ sở dữ liệu với lệnh Update-Database trong Package Manager Console.

Làm cách nào để ghi các dòng vào cửa sổ đầu ra trong studio trực quan?

tôi đã cố gắng:

try 
{ 
    context.SaveChanges(); 
} 
catch (System.Data.Entity.Validation.DbEntityValidationException e) 
{ 
    foreach (var eve in e.EntityValidationErrors) 
    { 
     System.Diagnostics.Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", 
      eve.Entry.Entity.GetType().Name, eve.Entry.State); 
     foreach (var ve in eve.ValidationErrors) 
     { 
      System.Diagnostics.Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"", 
       ve.PropertyName, ve.ErrorMessage); 
     } 
    } 
    throw; 
} 

Nhưng điều đó không làm việc. Bất kỳ đề xuất nào khác về cách gỡ lỗi này?

Trả lời

20

Tôi không biết tại sao việc ghi vào cửa sổ đầu ra VS không hoạt động và cách làm cho nó hoạt động. Nhưng như một phương sách cuối cùng, bạn chỉ cần ghi các lỗi vào một tệp văn bản sẽ hoạt động độc lập với loại ứng dụng mà bạn có:

try 
{ 
    context.SaveChanges(); 
} 
catch (System.Data.Entity.Validation.DbEntityValidationException e) 
{ 
    var outputLines = new List<string>(); 
    foreach (var eve in e.EntityValidationErrors) 
    { 
     outputLines.Add(string.Format(
      "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:", 
      DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State)); 
     foreach (var ve in eve.ValidationErrors) 
     { 
      outputLines.Add(string.Format(
       "- Property: \"{0}\", Error: \"{1}\"", 
       ve.PropertyName, ve.ErrorMessage)); 
     } 
    } 
    //Write to file 
    System.IO.File.AppendAllLines(@"c:\temp\errors.txt", outputLines); 
    throw; 

    // Showing it on screen 
    throw new Exception(string.Join(",", outputLines.ToArray())); 

} 
+0

Xin chào, wow, tại sao tôi không nghĩ vậy !? Cảm ơn! – Yustme

+2

@ Slauma: Cảm ơn bạn .. Nice trick .. Không bao giờ nghĩ về điều đó .. Đây là nơi mà sự khác biệt con số ra giữa các nhà phát triển tốt và những người rất tốt! ;) – user2394196

4

Bạn có thể chuyển nó lên ngăn xếp ngoại lệ như dưới đây.

try 
{ 
    _dbContext.SaveChanges(); 
} 
catch (DbEntityValidationException dbValEx) 
{ 
    var outputLines = new StringBuilder(); 
    foreach (var eve in dbValEx.EntityValidationErrors) 
    { 
    outputLines.AppendFormat("{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:" 
     ,DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State); 

    foreach (var ve in eve.ValidationErrors) 
    { 
     outputLines.AppendFormat("- Property: \"{0}\", Error: \"{1}\"" 
     ,ve.PropertyName, ve.ErrorMessage); 
    } 
    } 

throw new DbEntityValidationException(string.Format("Validation errors\r\n{0}" 
    ,outputLines.ToString()), dbValEx); 
} 
+0

Cảm ơn, tôi cũng sẽ thử điều này trong dự án tiếp theo của tôi! +1 – Yustme