public int Add(Person p) { using (var db = new FamilyContext()) { db.People.Add(p); db.SaveChanges(); return p.Id; } }
public void Delete(int id) { using (var db = new FamilyContext()) { Person p = new Person(); p.Id = id; db.People.Attach(p); db.Entry(p).State = System.Data.EntityState.Deleted; int n = db.SaveChanges(); Debug.Print("delete={0} rows", n); } }
public void Update(Person p) { using (var db = new FamilyContext()) { var personFromDb = db.People.Find(p.Id); // state->unchanged // if (!p.Equals(personFromDb)) { personFromDb.Name = p.Name; personFromDb.Gender = p.Gender; personFromDb.BirthYear = p.BirthYear; // } int n = db.SaveChanges(); Debug.Print("update={0} rows", n); } }
public void Update(Person p) { using (var db = new FamilyContext()) { var personFromDb = db.People.Find(p.Id); //if(!p.Equals(personFromDb)) // { //db.People.Attach(p); //db.Entry(p).State = System.Data.EntityState.Modified; personFromDb.Name = p.Name; personFromDb.Gender = p.Gender; personFromDb.BirthYear = p.BirthYear; //} //db.People.Attach(p); //db.Entry(p).State = System.Data.EntityState.Modified; int n = db.SaveChanges(); Debug.Print("update={0} rows", n); } }