【1】linq表达式

C#webapi(2)更新数据库

T_DATA_PE tp =new T_DATA_PE();
            int unit=int.Parse(context.Request.Form["Unit"]);
            int Style=int.Parse(context.Request.Form["style"]);
            int Community=int.Parse(context.Request.Form["community"]);
            DateTime Monitortime= Convert.ToDateTime(context.Request.Form["monitortime"]);
            var data = ef.FindAll<T_DATA_PE>(x => x.Unit == unit && x.style == Style && x.community == Community && x.monitortime == Monitortime).FirstOrDefault();
            tp = data;
            /*tp.Unit = data.Unit;
            tp.community = data.community;
            tp.style = data.style;
            tp.monitortime = data.monitortime;
            tp.image1 = data.image1;
            tp.image2 = data.image2;
            tp.image3 = data.image3;*/

【2】linq---update报错

C#webapi(2)更新数据库

https://www.cnblogs.com/godbell/p/7360739.html 

附加类型“AirQuality.Model.T_DATA_PE的实体失败,因为相同类型的其他实体已具有相同的主键值。在使用 "Attach" 方法或者将实体的状态设置为 "Unchanged" 或 "Modified" 时如果图形中的任何实体具有冲突键值,则可能会发生上述行为。这可能是因为某些实体是新的并且尚未接收数据库生成的键值。在此情况下,使用 "Add" 方法或者 "Added" 实体状态跟踪该图形,然后将非新实体的状态相应设置为 "Unchanged" 或 "Modified"。

 

 bool addresult = ef.Update<T_DATA_PE>(tp);更新数据

https://blog.csdn.net/huqitu/article/details/3765069?utm_source=blogxgwz7

https://blog.csdn.net/txhdjackie/article/details/77531745

/bool addresult = ef.Update<T_DATA_PE>(tp);//直接更新会报错
                ef.Delete<T_DATA_PE>(x => x.Unit == unit && x.style == Style && x.community == Community && x.monitortime == Monitortime);//删除原来的数据,然后再插入
                List<T_DATA_PE> list = new List<T_DATA_PE>();
                list.Add(tp);
                int addresult = ef.Insert<T_DATA_PE>(list);
               
                if (addresult >0)

【3】null.ToString()抛出异常

result.postid = temp.Rectification1.ToString() + " ; " + temp.Rectification2.ToString() + " ; " + temp.Rectification3.ToString() + " ; ";

null.ToString() 抛出异常

/// <summary>
        /// 曝光问题解决操作页面
        /// </summary>
        /// <returns></returns>
        [HttpGet, HttpPost, Route("SolveProblem")]
        public PostResult SolveProblem() 
        {
            PostResult result = new PostResult();
            HttpContext context = HttpContext.Current;
            //foreach (var key in context.Request.Form.AllKeys){}
            T_DATA_PE tp =new T_DATA_PE();
            var data = ef.FindAll<T_DATA_PE>(x => x.Unit == (int.Parse(context.Request.Form["Unit"])) && x.style == (int.Parse(context.Request.Form["style"])) && x.community == (int.Parse(context.Request.Form["community"])) && x.monitortime == Convert.ToDateTime(context.Request.Form["monitortime"])).FirstOrDefault();
            tp.Unit = data.Unit;
            tp.community = data.community;
            tp.style = data.style;
            tp.monitortime = data.monitortime;
            tp.image1 = data.image1;
            tp.image2 = data.image2;
            tp.image3 = data.image3;
            tp.Operator = context.Request.Form["Operator"];//解决人
            tp.status = 1;//解决状态
            tp.modifytime = Convert.ToDateTime(context.Request.Form["modifytime"]);
            tp.solution = context.Request.Form["solution"];
            
            try
            {
                System.Web.Mvc.Controller controller = new HomeController();

                #region 上传图片
                string path = "\\";// context.Request.Form["path"]
                
                //用“,”分割文件流
                var base64Code = context.Request.Form["base64Code1"].Split(',');
                for (var i = 0; i < base64Code.Length; i++)
                {
                    string str = base64Code[i];
                    string PicPath = System.Configuration.ConfigurationManager.AppSettings["PicPath"];
                    string fileExt = "jpg"; //文件扩展名,不含“.” 
                    string newFileName = GetTimeStamp() + "_" + (i + 1) + "." + fileExt; //随机生成新的文件名
                    string pathY = System.Web.HttpContext.Current.Server.MapPath(path + "UploadImg\\solution\\yt\\");
                    if (!Directory.Exists(pathY))
                    {
                        Directory.CreateDirectory(pathY);
                    }
                    //保存图片路径--绝对路径
                    string newFilePath = System.Web.HttpContext.Current.Server.MapPath(path + "UploadImg\\solution\\yt\\" + newFileName);
                    //判断一下是否上传了图片
                    if (str != null)
                    {
                        Image img = Base64ToImg(str);//Image--using System.Drawing;
                        img.Save(newFilePath, System.Drawing.Imaging.ImageFormat.Png);
                        string ypath = PicPath + "UploadImg/solution/yt/" + newFileName;
                        if (i == 0)
                        {
                            tp.Rectification1 = ypath;
                        }
                        else if (i == 1)
                        {
                            tp.Rectification2 = ypath;
                        }
                        else if (i == 2)
                        {
                            tp.Rectification3 = ypath;
                        }

                    }
                }
                                                              

                List<T_DATA_PE> list = new List<T_DATA_PE>();
                list.Add(tp);
                bool addresult = ef.Update<T_DATA_PE>(tp);
                if (addresult ==true)
                {
                    T_DATA_PE temp = ef.FindAll<T_DATA_PE>(p => p.Unit == tp.Unit && p.style == tp.style && p.community == tp.community).OrderByDescending(p => p.monitortime).FirstOrDefault();
                    result.flag = true;
                    result.msg = "发布成功";
                    result.postid = temp.Rectification1.ToString() + " ; " + temp.Rectification2.ToString() + " ; " + temp.Rectification3.ToString() + " ; ";
                }
                else
                {
                    result.flag = false;
                    result.msg = "发布失败";
                    result.postid = "";
                }
            }
            catch (Exception e)
            {

                result.flag = false;
                result.msg = "接口异常:" + e.ToString();
                result.postid = "";
            }
            return result;
        }

    /// <summary>
    /// 增加巡查返回结果
    /// </summary>
    public class PostResult
    {
        public bool flag { get; set; }
        public string msg { get; set; }
        public string postid { get; set; }
    }

 

相关文章: