我有一个基本保存更改功能,但它没有工作,它没有返回错误,只是没有写入数据库。
Appointment _appointment = new Appointment();
int errorCount = 0;
Patient _patient = new Patient();
_patient = SourceDal.getPatientByPatientId(txtPatientId.Text);
_patient.SSN = txtSSN.Text;
_patient.FirstName = txtPatientFirstName.Text;
_patient.LastName = txtPatientLastName.Text;
_patient.Middle = txtPatientMiddle.Text;
_patient.AddressOne = txtPatientAddressOne.Text;
_patient.City = txtPatientCity.Text;
_patient.State = txtPatientState.Text;
_patient.ZipCode = txtPatientZip.Text;
_patient.HomePhone = txtPatientHomePhone.Text;
_patient.WorkPhone = txtPatientWorkPhone.Text;
_patient.CellPhone = txtPatientCellPhone.Text;
if (rBtnHomePhone.Checked == true)
_patient.ApptPhone = txtPatientHomePhone.Text;
if (rBtnHomePhone.Checked == true)
_patient.ApptPhone = txtPatientHomePhone.Text;
if (rBtnWorkPhone.Checked == true)
_patient.ApptPhone = txtPatientWorkPhone.Text;
_patient.BirthDate = dtBirthDate.DateTime;
_patient.emailAddress = txtPatientEmail.Text;
_patient.Race = (int)dpRace.SelectedValue;
_patient.Ethnicity = (int)dpEthnicity.SelectedValue;
_patient.Language = (int)dpLanguages.SelectedValue;
_patient.AlertNote = txtPatientNotes.Text;
if (dpGender.Text == "")
{
dpGender.Focus();
errorCount = 1;
lblGenderRequired.Text = "* Gender is required.";
}
else
{
errorCount = 0;
lblGenderRequired.Visible = false;
}
_patient.Gender = dpGender.Text.Substring(0, 1);
_patient.PatientID = txtPatientId.Text;
txtPatientFirstName.Text = _patient.FirstName;
txtPatientLastName.Text = _patient.LastName;
if(updateOrSave == false) // IF ITS SAVE NEW GO AHEAD ADD IT TO THE CONTEXT.
SourceDal.AddToPatient(_patient);
SourceDal.SaveChanges();
我的源dal由继承上下文基础的函数组成,具有以下
public abstract int SaveChanges();
protected int SaveChanges(System.Data.Entity.Core.Objects.ObjectContext context)
{
try
{
int i = context.SaveChanges();
if (i == 0)
return 0;
return 1;
}
catch (Exception ex)
{
throw new EntityContextException("SaveChanges failed.", ex);
}
}
没有错误,所以不会出现什么问题我将SourceDal声明为
SourceContext SourceDal = new SourceContext();
这是我在源头上的保存行为
public override int SaveChanges()
{
try
{
using (var myContext = new SMBASchedulerEntities(this.Connectionstring))
{
return myContext.SaveChanges();
}
}
catch (DbEntityValidationException e)
{
foreach (var eve in e.EntityValidationErrors)
{
Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name, eve.Entry.State);
foreach (var ve in eve.ValidationErrors)
{
Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName, ve.ErrorMessage);
}
}
throw;
}
}
修改2
我甚至尝试了以下操作,但仍然没有保存患者记录,AddToPaient工作正常
public void SavePatient(Patient _patient)
{
try {
using (var myContext = new SMBASchedulerEntities(this.Connectionstring))
{
myContext.Entry(_patient).State = System.Data.Entity.EntityState.Modified;
myContext.SaveChanges();
}
}
catch (Exception EX)
{ }
}