• EF 简单的 CRUD、分页 代码笔记


    添加:
     static void Main(string[] args)
            {
                CCDBEntities ccdbContext = new CCDBEntities();

                MyStudent user = new MyStudent();
      
                user.FName = "胡章诚";
                user.FAge = 21;
                user.FGender = "男";
                user.FMath = 88;
                user.FEnglish = 99;
                user.FClassId = 2;
                user.FBirthday = DateTime.Now;

               
                ccdbContext.MyStudent.Add(user);
                ccdbContext.SaveChanges();
            }


    -----------------------------------
    修改整个表实体

     static void Main(string[] args)
            {
                CCDBEntities ccdbContext = new CCDBEntities();

                MyStudent user = new MyStudent();
                user.Fid = 21;   //注:修改的时候要为主键赋值
                user.FName = "胡章诚";
                user.FAge = 21;
                user.FGender = "男";
                user.FMath = 88;
                user.FEnglish = 99;
                user.FClassId = 2;
                user.FBirthday = DateTime.Now;

                ccdbContext.MyStudent.Attach(user);
                ccdbContext.Entry<MyStudent>(user).State = System.Data.EntityState.Modified;
                ccdbContext.SaveChanges();
            }


    ----------------------------------------

    修改一个列:
     static void Main(string[] args)
            {
                CCDBEntities ccdbContext = new CCDBEntities();

                MyStudent user = new MyStudent();
                user.Fid = 22;
                user.FName = "胡章诚";
                user.FAge = 21;
                user.FGender = "男";
                user.FMath = 88;
                user.FEnglish = 99;
                user.FClassId = 2;
                user.FBirthday = DateTime.Now;

                ccdbContext.MyStudent.Attach(user);
                ccdbContext.Entry<MyStudent>(user).Property<string>(u => u.FName).IsModified = true;
                ccdbContext.SaveChanges();

                Console.WriteLine("修改成功");
                Console.ReadKey();
            }
    -------------------------------------------
    删除一条数据:
      static void Main(string[] args)
            {
                CCDBEntities ccdbContext = new CCDBEntities();

                MyStudent user = new MyStudent();
                user.Fid = 22;

                ccdbContext.MyStudent.Attach(user);
                ccdbContext.Entry<MyStudent>(user).State = System.Data.EntityState.Deleted;
                ccdbContext.SaveChanges();
            }


    -----------------------------------------

    查询:

    1、用Lambda进行查询

                var modelList = dbContext.MyStudent
                    .Where(u => u.Fid < 100)
                    .Where(u => u.Fid > 40)
                    .Select(u => new { u.Fid, u.FName, u.FGender });


    2、用linq查询
                var modelList = from u in dbContext.MyStudent
                                where u.Fid > 40 && u.Fid < 100
                                select new { u.FGender, u.Fid };

    --------------------------------------

    分页:

    采用 Lambda表达式 分页:
                var modelList = dbContext.MyStudent
                    .OrderBy(u=>u.Fid)
                    .Skip(pagesize * (pageIndex - 1))
                    .Take(pagesize);


     

  • 相关阅读:
    食谱
    食谱
    食谱
    无题
    重要通知
    幼儿食谱
    安卓应用开发常用代码
    安卓开发环境搭建
    《浪潮之巅》读书笔记——第11章 摩托罗拉
    预制redis数据
  • 原文地址:https://www.cnblogs.com/key1309/p/3379685.html
Copyright © 2020-2023  润新知