C# SQL Server EF框架增删改查

            //创建数据库对象
            UserEntities user = new UserEntities();

            //增加
            user.Card.Add(new Card { ID = "15", UserName = "admin" });
            user.SaveChanges();

            //删除
            Card card= user.Card.FirstOrDefault(x => x.ID == "15");
            user.Card.Remove(card);
            user.SaveChanges();

            //修改
            Card card = user.Card.FirstOrDefault(x => x.ID == "15");
            card.UserName = "admin";
            user.Entry(card).State= System.Data.Entity.EntityState.Modified;
            user.SaveChanges();

            //查询
            List<Card> cardList = user.Card.ToList();//全部查询
            List<Card> cardWhereList = user.Card.Where(x => x.ID == "15").ToList();//条件查询
            Card cardOnly = user.Card.Find(1);//拿第一个

            //排序
            List<Card> result = user.Card
                .OrderBy(a => a.ID)//按ID排序//OrderByDescending降序
                .Skip(1)//跳过一个
                .Take(2)//拿两个
                .ToList();

上一篇:The 2021 ICPC Asia Regionals Online Contest (II) L Euler Function


下一篇:力扣-142 环形链表Ⅱ