当前位置:Gxlcms > mysql > 显示EF实体对象的详细错误信息db.Entry(entity).GetValidationR

显示EF实体对象的详细错误信息db.Entry(entity).GetValidationR

时间:2021-07-01 10:21:17 帮助过:17人阅读

方法一:在快速监视中查看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

方法一:在快速监视中查看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;
            }
        }
    }


快速监视中查看: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;
            }
        }
    }

人气教程排行