API EF 增删改查


DbContext基类
public class SchoolContext:DbContext { public SchoolContext() : base("school") { }//连接字符串 public virtual DbSet<School> School { get; set; } }

 

  public class SchoolDal
    {
        SchoolContext db = new SchoolContext();
        //添加
        public int Add(School model)
        {
            db.School.Add(model);
            return db.SaveChanges();
        }
        //显示
        public List<School> Show()
        {
            return db.School.ToList();
        }
        //删除
        public int Del(int id) {
            School model = db.School.Where(s => s.Id == id).FirstOrDefault();
            db.School.Remove(model);
            return db.SaveChanges();
        }

        //修改
        public int Upt(School model)
        {
            db.School.Attach(model);
            db.Entry(model).State = System.Data.Entity.EntityState.Modified;
            return db.SaveChanges();
        }
    }

 

posted @ 2020-02-17 17:02  Chen0110  阅读(182)  评论(0编辑  收藏  举报