EF简单的添加修改删除基本语法

时间:2022-07-26 01:24:55

      using ( androidhiveEntities db = new androidhiveEntities() )
                {

                    #region 添加
                    //aa a = new aa() { endDate =DateTime.Now,name="张三",doccode="ddoccode ",startDate=DateTime.Now };
                    //db.aa.Add(a);
                    //db.SaveChanges();
                    #endregion

                    #region 修改

                    //修改
                    var a1 = (from u in db.aa where u.id == 25 select u).FirstOrDefault();
                    a1.name = "李晓四";
                    var a = (from u in db.aa where u.id == 27 select u).FirstOrDefault();
                    a.name  = "张晓三";
                    db.SaveChanges();
                    #endregion

                    #region 删除
                    //var a = (from u in db.aa where u.id == 26 select u).First();
                    //db.aa.Remove(a);

                    //db.SaveChanges();
                    #endregion
                }