zoukankan      html  css  js  c++  java
  • 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);


     

  • 相关阅读:
    压缩自编码器具备天然的模式过滤能力
    自编码器天生具备压缩、去噪和生成新样本的能力
    1338. Reduce Array Size to The Half
    442. Find All Duplicates in an Array
    1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold
    1437. Check If All 1's Are at Least Length K Places Away
    695. Max Area of Island
    1414. Find the Minimum Number of Fibonacci Numbers Whose Sum Is K
    1233. Remove Sub-Folders from the Filesystem
    238. Product of Array Except Self
  • 原文地址:https://www.cnblogs.com/key1309/p/3379685.html
Copyright © 2011-2022 走看看