private void insertSeta(LookupSeta setaObj) { using (var Dbconnection = new Impendulo.Data.Models.MCDEntities()) { Dbconnection.LookupSetas.Add(setaObj); Dbconnection.SaveChanges(); }; }
private void SetContorls() { if (setaBindingSource.Count > 0) { LookupSeta setaObj = (LookupSeta)(setaBindingSource.Current); txtSeta.Text = setaObj.SetaAbbriviation; txtSetaName.Text = setaObj.SetsName; btnAddEditSeta.Text = "Update"; } else { this.ClearControls(); btnAddEditSeta.Text = "Add"; } }
private void btnLinkSeta_Click(object sender, EventArgs e) { using (var DbConnection = new MCDEntities()) { /* * this steps follow to both entities * * 1 - create instance of entity with relative primary key * * 2 - add instance to context * * 3 - attach instance to context */ LookupSeta s = (LookupSeta)bindingSourceAvailableSeta.Current; //1 var SetaObj = new LookupSeta { SetaID = s.SetaID }; //2 DbConnection.LookupSetas.Add(SetaObj); //3 DbConnection.LookupSetas.Attach(SetaObj); // 1 CurriculumCourse CurriculumCourseObj = new CurriculumCourse { CurriculumCourseID = this.CurriculumCourseID }; //// 2 DbConnection.CurriculumCourses.Add(CurriculumCourseObj); //// 3 DbConnection.CurriculumCourses.Attach(CurriculumCourseObj); // like previous method add instance to navigation property CurriculumCourseObj.LookupSetas.Add(SetaObj); // call SaveChanges DbConnection.SaveChanges(); } this.populateAvailableSeta(); this.populateLinkedSeta(); this.setAddRemoveControls(); }
private void btnLinkSeta_Click(object sender, EventArgs e) { using (var DbConnection = new MCDEntities()) { /* * this steps follow to both entities * * 1 - create instance of entity with relative primary key * * 2 - add instance to context * * 3 - attach instance to context */ LookupSeta s = (LookupSeta)bindingSourceAvailableSeta.Current; //1 var SetaObj = new LookupSeta { SetaID = s.SetaID }; //2 DbConnection.LookupSetas.Add(SetaObj); //3 DbConnection.LookupSetas.Attach(SetaObj); // 1 //TrainingDepartmentCourseMetaData TDCMD = new TrainingDepartmentCourseMetaData //{ // TrainingDepartmentCourseMetaDataID = this.TrainingDepartmentCourseMetaDataID //}; // 2 // DbConnection.TrainingDepartmentCourseMetaDatas.Add(TDCMD); // 3 //DbConnection.TrainingDepartmentCourseMetaDatas.Attach(TDCMD); // like previous method add instance to navigation property //TDCMD.Setas.Add(SetaObj); // call SaveChanges DbConnection.SaveChanges(); } this.populateAvailableSeta(); this.populateLinkedSeta(); this.setAddRemoveControls(); }
private void btnRemoveSeta_Click(object sender, EventArgs e) { using (var DbConnection = new MCDEntities()) { ////// return one instance each entity by primary key LookupSeta s = (LookupSeta)bindingSourceLinkedSeta.Current; var SetaObj = DbConnection.LookupSetas.FirstOrDefault(a => a.SetaID == s.SetaID); var CurriculumCourseObj = DbConnection.CurriculumCourses.FirstOrDefault(a => a.CurriculumCourseID == this.CurriculumCourseID); ////// call Remove method from navigation property for any instance CurriculumCourseObj.LookupSetas.Remove(SetaObj); ////// call SaveChanges from context DbConnection.SaveChanges(); } this.populateAvailableSeta(); this.populateLinkedSeta(); this.setAddRemoveControls(); }
private void btnAddEditSeta_Click(object sender, EventArgs e) { if (btnAddEditSeta.Text.ToLower().Equals("add")) { /*Step 1 * GEt Seta Values ***********************************************/ var newSetaObj = new LookupSeta { SetsName = txtSetaName.Text, SetaAbbriviation = txtSeta.Text }; /*Step 2 * Insert Seta Values into Database ***********************************************/ this.insertSeta(newSetaObj); /*Step 3 * Refresh Gridview ***********************************************/ this.refreshGridView(); /*Step 4 * Set Seta Text Controls ***********************************************/ this.SetContorls(); } else { } //if (btnAddEditSeta.Text.ToLower().Equals("add")) //{ // using (var Dbconnection = new MCDEntities()) // { // var newSetaObj = new Seta // { // SetsName = txtSetaName.Text, // SetaAbbriviation = txtSeta.Text // }; // Dbconnection.Setas.Add(newSetaObj); // Dbconnection.SaveChanges(); // this.refreshGridView(); // this.SetContorls(); // btnAddEditSeta.Text = "Update"; // }; //} //else //{ // using (var Dbconnection = new MCDEntities()) // { // Seta setaObj = (Seta)(setaBindingSource.Current); // setaObj.SetsName = txtSetaName.Text; // setaObj.SetaAbbriviation = txtSeta.Text; // Dbconnection.Entry(setaObj).State = System.Data.Entity.EntityState.Modified; // Dbconnection.SaveChanges(); // this.refreshGridView(); // this.SetContorls(); // }; //} }