方法一:在快速監(jiān)視中查看db.Entry(entity).GetValidationResult() public class NewsDal { public static News Add(News entity) { using (CmsContext db = new CmsContext()) { News news = new News(); try { news = db.News.Add(entity); db.SaveChanges
方法一:在快速監(jiān)視中查看db.Entry(entity).GetValidationResult()
public class NewsDal { public static News Add(News entity) { using (CmsContext db = new CmsContext()) { News news = new News(); try { news = db.News.Add(entity); db.SaveChanges(); } catch (Exception ex) { throw new Exception(ex.Message); } return news; } } }
快速監(jiān)視中查看:db.Entry(entity).GetValidationResult()
方法二:直接catch (DbEntityValidationException ex) 前提先引用:using System.Data.Entity.Validation;
public class NewsDal { public static News Add(News entity) { using (CmsContext db = new CmsContext()) { News news = new News(); try { news = db.News.Add(entity); db.SaveChanges(); } //catch (Exception ex) catch (DbEntityValidationException ex) { throw new Exception(ex.Message); } return news; } } }
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com