asp.net mvc4 在EF新增的时候报对一个实体或多个实体验证失败

//entity为空 是数据库上下文会验证实体验证
                var entity = db.UserInfo.Where(u => u.Mobile == mobile).FirstOrDefault();

                

                if (entity != null)
                {
               
  
                    MobileLogModel.AddMobileLog(dvid, dvType, "用户注册", "手机已注册", "UserInfo-Register");

                    return Json(result, JsonRequestBehavior.AllowGet);
                }


  public static void AddMobileLog( string title, string msg)
        {
            using (Dao.GiftDBEntities db = new Dao.GiftDBEntities())
            {
               //让db 不对实体验证
                db.Configuration.ValidateOnSaveEnabled = false;

                Dao.MobileLog log = new Dao.MobileLog();

                log.MobileLogID = Utility.GetGUID();
           
            
                log.Createtime = DateTime.Now;
                log.Title = title;
                log.Content = msg;

                db.MobileLog.Add(log);

                db.SaveChanges();

    //再让db对实体验证
                db.Configuration.ValidateOnSaveEnabled = true;
            }

        }        

 

posted @ 2015-04-25 13:47  大空白纸  阅读(344)  评论(0编辑  收藏  举报