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

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

WBOY
Release: 2016-06-07 15:00:38
Original
1210 people have browsed it

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


快速监视中查看:db.Entry(entity).GetValidationResult() 


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


方法二:直接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;
            }
        }
    }
Copy after login

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



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template