Esempio n. 1
0
        //当删除按钮点击的时候
        protected void Button3_Click(object sender, EventArgs e)
        {
            var db = new myTestDatabaseEntities();
            //var memberList = from mem in db.MemberInfo
            //                 where mem.miid == 28
            //                 select mem;
            ////返回第一个元素或者null(没有元素的时候)  FirstOrDefault();
            //MemberInfo mbi = memberList.FirstOrDefault();
            //if (mbi != null)
            //{
            //    //删除查出来的数据
            //    //db.MemberInfo.Remove(mbi);
            //    //通过枚举标记数据的状态  标记为删除状态
            //    db.Entry<MemberInfo>(mbi).State = System.Data.Entity.EntityState.Deleted;
            //    //更新到数据库中
            //    db.SaveChanges();
            //}
            //else
            //{
            //    Response.Write("删除的数据不存在");
            //}

            //方法2  常用
            MemberInfo mbi = new MemberInfo()
            {
                miid = 29
            };

            db.Entry <MemberInfo>(mbi).State = System.Data.Entity.EntityState.Deleted;
            db.SaveChanges();
        }
Esempio n. 2
0
        //更新按钮点击的时候
        protected void Button4_Click(object sender, EventArgs e)
        {
            var db         = new myTestDatabaseEntities();
            var memberList = from mem in db.MemberInfo where mem.miid == 31 select mem;
            var mbi        = memberList.FirstOrDefault();

            mbi.miphone = "新的phone新的phone";
            //把状态标记为更新
            db.Entry <MemberInfo>(mbi).State = EntityState.Modified;
            db.SaveChanges();
        }