//Add Method public int Program_Add(Program item) { //input is an instance of all data for an entity //one could send in individual values in separate // parameters BUT eventaully, they would need // to be place in an instance of the entity //changes to the database should be done in a transaction using (var context = new Star_TEDContext()) { //stage new record to DbSet<T> for commitment to // the database //the new record is not YET physically on the // database context.Programs.Add(item); //commit the staged record to the database table //if this statement is NOT executed then the // insert is not completed on the database table (Rollback) //the new identity value is created on the successful // execution of the statement //the identity value is NOT available until the execution // of the statement is complete //during execution of this statemtent ANY entity validation // annotation is executed context.SaveChanges(); //optionally one could return the new identity value // after the SaveChanges has been done return(item.ProgramID); } }
public int Program_Delete(int schoolcode) { //if you wish to return the number of rows affected // your rdt should be an int; otherwise use a void using (var context = new Star_TEDContext()) { var existing = context.Programs.Find(schoolcode); context.Programs.Remove(existing); return(context.SaveChanges()); } }
public int Program_Update(Program item) { //if you wish to return the number of rows affected // your rdt should be an int; otherwise use a void //action is done as a transaction using (var context = new Star_TEDContext()) { //stage the ENTIRE record for saving //all fields are altered context.Entry(item).State = System.Data.Entity.EntityState.Modified; //capture the number of rows affected for the update // commit and return return(context.SaveChanges()); } }