2014年11月21日 星期五

[C#] Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.


form: http://ianworkshop.blogspot.tw/2014/01/validation-failed-for-one-or-more.html


可能某個不可為null的欄位, 填入了null, 但是不知道是哪個欄位, 因此使用try catch來捕抓錯誤

            try
            {
                _db.SaveChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}"validationError.PropertyNamevalidationError.ErrorMessage);
                    }
                }
            }

0 意見:

張貼留言

 

MangoHost Copyright © 2009 Cookiez is Designed by Ipietoon for Free Blogger Template