Ejemplo n.º 1
0
        static void DeleteStudents()
        {
            //根据主键构建一个对象
            Students student = new Students
            {
                StudentId = 100000
            };

            //第一种写法:通过设置状态
            using (StudentManageDBEntities db = new StudentManageDBEntities())
            {
                db.Set <Students>().Attach(student);
                db.Entry(student).State = EntityState.Deleted;
                db.SaveChanges();
            }

            //第二种写法:通过remove方法
            using (StudentManageDBEntities db = new StudentManageDBEntities())
            {
                db.Set <Students>().Attach(student);
                db.Students.Remove(student);
                db.SaveChanges();//跟新到数据库(自动生成SQL语句)
            }

            //第三种写法:先查寻,再删除

            using (StudentManageDBEntities db = new StudentManageDBEntities())
            {
                var stu = db.Students.Where(o => o.StudentId == 100000).FirstOrDefault();
                db.Students.Remove(stu);
                db.SaveChanges();//跟新到数据库(自动生成SQL语句)
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 编辑:修改
        /// </summary>
        static void Modify()
        {
            //【1】数据验证
            //【2】对象封装
            Students student = new Students
            {
                StudentId      = 100012,
                StudentName    = "龚均强",
                Gender         = "男",
                Birthday       = Convert.ToDateTime("1991-04-22"),
                StudentIdNo    = 510722199104221978,
                CardNo         = "0000000",
                Age            = 28,
                PhoneNumber    = "18408230331",
                StudentAddress = "西华大学",
                ClassId        = 2,
            };
            //【3】创建数据库上下文对象
            StudentManageDBEntities db = new StudentManageDBEntities();

            db.Students.Attach(student);
            db.Entry(student).State = EntityState.Modified; //设置对象的状态为修改
            int result = db.SaveChanges();                  //跟新到数据库(自动生成SQL语句)

            Console.WriteLine(result);
            Console.WriteLine("学号:" + student.StudentId);
        }
Ejemplo n.º 3
0
        static void UpdateStudents()
        {
            Students student = new Students
            {
                StudentName    = "龚均强",
                Gender         = "男",
                Birthday       = Convert.ToDateTime("1991-04-22"),
                StudentIdNo    = 510722199104221978,
                CardNo         = "1958141",
                Age            = 28,
                PhoneNumber    = "18408230331",
                StudentAddress = "西华大学",
                ClassId        = 1,
            };

            //第一种写法:构建全部属性对象修改
            using (StudentManageDBEntities db = new StudentManageDBEntities())
            {
                //db.Students.Attach(student);//可以省略,将student实体添加到上下文中
                db.Entry(student).State = EntityState.Modified;
                db.SaveChanges();
            }

            //第二种写法:先查询再修改
            using (StudentManageDBEntities db = new StudentManageDBEntities())
            {
                //查询对象
                var stu = db.Students.FirstOrDefault(o => o.StudentId == 100012);
                //修改对象属性
                stu.StudentAddress = "红哇小区";
                //提交修改
                db.SaveChanges();//跟新到数据库(自动生成SQL语句)
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 新增对象
        /// </summary>
        static void AddModel()
        {
            //【1】数据验证
            //【2】对象封装
            Students student = new Students
            {
                StudentName    = "龚均强",
                Gender         = "男",
                Birthday       = Convert.ToDateTime("1991-04-22"),
                StudentIdNo    = 510722199104221978,
                CardNo         = "1958141",
                Age            = 28,
                PhoneNumber    = "18408230331",
                StudentAddress = "西华大学",
                ClassId        = 1,
            };
            //【3】创建数据库上下文对象
            StudentManageDBEntities db = new StudentManageDBEntities();

            db.Students.Add(student);      //将对象添加到缓存中
            int result = db.SaveChanges(); //跟新到数据库(自动生成SQL语句)

            Console.WriteLine(result);
            Console.WriteLine("学号:" + student.StudentId);
        }
Ejemplo n.º 5
0
        static void AddStudents()
        {
            Students student = new Students
            {
                StudentName    = "龚均强",
                Gender         = "男",
                Birthday       = Convert.ToDateTime("1991-04-22"),
                StudentIdNo    = 510722199104221978,
                CardNo         = "1958141",
                Age            = 28,
                PhoneNumber    = "18408230331",
                StudentAddress = "西华大学",
                ClassId        = 1,
            };

            //第一种写法:
            using (StudentManageDBEntities db = new StudentManageDBEntities())
            {
                db.Entry(student).State = EntityState.Added;
                db.SaveChanges();
            }

            //第二种写法:
            using (StudentManageDBEntities db = new StudentManageDBEntities())
            {
                //db.Students.Attach(student);//可以省略,将student实体添加到上下文中
                db.Students.Add(student);
                db.SaveChanges();
            }
        }
Ejemplo n.º 6
0
        //状态跟踪类型测试
        static void Status()
        {
            StudentManageDBEntities db = new StudentManageDBEntities();
            //创建一个对象:当前对象的状态为detached
            Students student = new Students
            {
                StudentName    = "龚均强",
                Gender         = "男",
                Birthday       = Convert.ToDateTime("1991-04-22"),
                StudentIdNo    = 520722199104221978,
                CardNo         = "2958141",
                Age            = 28,
                PhoneNumber    = "18408230331",
                StudentAddress = "西华大学",
                ClassId        = 1,
            };

            Console.WriteLine(db.Entry(student).State.ToString());
            //添加到缓存对象中:状态为Added
            db.Students.Add(student);
            Console.WriteLine(db.Entry(student).State.ToString());
            //保存后:UnChanged
            db.SaveChanges();
            Console.WriteLine(db.Entry(student).State.ToString());
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 线查找对象再删除对象
        /// </summary>
        static void Delete()
        {
            StudentManageDBEntities db = new StudentManageDBEntities();
            //查询对象
            var student = db.Students.FirstOrDefault(o => o.StudentId == 100012);

            //从缓存中移除对象删除
            db.Students.Remove(student);
            //提交
            int result = db.SaveChanges();//跟新到数据库(自动生成SQL语句)

            Console.WriteLine("影响行数:" + result);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 部分字段的修改
        /// </summary>
        static void Modify1()
        {
            StudentManageDBEntities db = new StudentManageDBEntities();
            //查询对象
            var stu = db.Students.FirstOrDefault(o => o.StudentId == 100012);

            //修改对象属性
            stu.StudentAddress = "红哇小区";
            //提交修改
            int result = db.SaveChanges();//跟新到数据库(自动生成SQL语句)

            Console.WriteLine("影响行数:" + result);
        }
Ejemplo n.º 9
0
        static void Delete2()
        {
            StudentManageDBEntities db = new StudentManageDBEntities();
            Students student           = new Students
            {
                StudentId = 100013,
            };

            db.Set <Students>().Attach(student);
            db.Entry(student).State = EntityState.Deleted;

            //提交
            int result = db.SaveChanges();//跟新到数据库(自动生成SQL语句)

            Console.WriteLine("影响行数:" + result);
        }