static List <Student> Get() { using (var ctx = new EFCoreDemoDbContext()) { return(ctx.Student.Where(p => p.FirstName == "zhang").ToList()); } }
static void Update() { using (var ctx = new EFCoreDemoDbContext()) { var student = ctx.Student.First(p => p.FirstName == "zhang"); if (student != null) { student.LastName = "san_new"; ctx.SaveChanges(); } } }
static void Delete() { using (var ctx = new EFCoreDemoDbContext()) { var student = ctx.Student.FirstOrDefault(p => p.FirstName == "li"); if (student != null) { ctx.Student.Remove(student);//或者:ctx.Remove<Student>(student); ctx.SaveChanges(); } } }
static void Add() { using (var ctx = new EFCoreDemoDbContext()) { var student = new Student { FirstName = "li", LastName = "si" }; ctx.Student.Add(student);//或:ctx.Add<Student>(student); ctx.SaveChanges(); } }