我在这种特定方法中遇到了问题。我第一次保存它的方法是在 AppointmentEvent 中创建一条记录并在 Appointment 中更改已验证的变量,但它会再次创建用户记录。
private void CreateAppointment(Employee user)
{
try
{
using (var db = new CosmeticContext())
{
Appointment appointment = db.Appointment.FirstOrDefault(o => o.name == phoneNumber.name && o.surname == phoneNumber.surname && o.verified == false);
appointment.verified = true;
PhoneNumber ph = db.PhoneNumber.FirstOrDefault(o => o.name == phoneNumber.name && o.surname == phoneNumber.surname);
db.PhoneNumber.Remove(ph);
db.AppointmentEvent.Add(new AppointmentEvent
{
date = DateTime.Now,
Employees = user,
Appointments = appointment,
EventComment = EventComment.Save
});
db.SaveChanges();
}
}
catch (System.Data.Entity.Validation.DbEntityValidationException e)
{
DbScript.ShowEntityErrors(e);
}
}
当我第二次运行它时,它会使用以前的值复制用户、约会事件和约会记录。当我通过调试器时,我看到 db.AppointmentEvent.Local 有 0 条记录,但第二次 Add 方法在 db.AppointmentEvent.Local 中创建了 2 条记录。我不明白一种 Add 方法如何添加 2 条记录。谷歌在为我展示正确的案例方面没有帮助,但 using 块不应该正确处理所有内容吗?
以下是一些包含此处使用的对象的附加类:
public class Employee
{
[Key]
public int employeeId { get; set; }
[Required, MaxLength(25), MinLength(4)]
public string userName { get; set; }
[Required, MaxLength(25), MinLength(4)]
public string firstName { get; set; }
[Required, MaxLength(25), MinLength(4)]
public string lastName { get; set; }
[Required, MaxLength(100), MinLength(20)]
public string password { get; set; }
[Required]
public EmployeeType EmployeeType { get; set; }
public virtual List<PhoneNumber> PhoneNumbers { get; set; }
public virtual List<AppointmentEvent> AppointmentEvents { get; set; }
}
public class AppointmentEvent
{
public int appointmentEventId { get; set; }
[Required]
public DateTime date { get; set; }
[Required]
public virtual Employee Employees { get; set; }
[Required]
public virtual Appointment Appointments { get; set; }
[Required]
public EventComment EventComment { get; set; }
}
public class Appointment
{
[Key]
public int appointmentId { get; set; }
[Required, MaxLength(20)]
public string name { get; set; }
[Required, MaxLength(30)]
public string surname { get; set; }
[Required, MaxLength(20), Phone]
public string phone { get; set; }
[Required]
public DateTime date { get; set; }
[Required]
public ServiceType ServiceType { get; set; }
[Required]
public bool verified { get; set; }
public virtual List<AppointmentEvent> AppointmentEvents { get; set; }
}
编辑:忘了告诉我正在创建一个 Windows 窗体应用程序和更多代码,它显示了用户部分。:这是 Form nr2 。:(我的记录有问题)
public partial class CallCenter : Form
{
Employee employee;
static ListTime listTime = new ListTime();
Button pushedButton = new Button();
public CallCenter(Employee loginEmployee)
{
employee = loginEmployee;
InitializeComponent();
tableLayoutPanel2.CellPaint += tableLayoutPanel1_CellPaint;
GeneratedServiceTypeComboBox.DataSource = Enum.GetValues(typeof(ServiceType));
textBox1.Text = DateTime.Now.AddDays(1).Date.ToString("dd.MM.yyyy");
textBox2.Text = DateTime.Now.AddDays(2).Date.ToString("dd.MM.yyyy");
RefreshAppointmentAvailabilityTable();
}
private void SaveButton_Click(object sender, EventArgs e)
{
try
{
GenerateButton.Enabled = true;
SaveButton.Enabled = false;
this.NameBox.Text = string.Empty;
SurnameBox.Text = string.Empty;
PhoneBox.Text = string.Empty;
GeneratedNameBox.Text = string.Empty;
GeneratedSurnameBox.Text = string.Empty;
GeneratedPhoneBox.Text = string.Empty;
pushedButton.BackColor = Color.LightGreen;
ChangeAppointmentAvailabilityButtonsStatus(false);
CreateAppointment(employee);
}
catch (Exception f)
{ MessageBox.Show(f.ToString()); }
}
...
}
这是来自form1:(登录屏幕)
private void LoginButton_Click(object sender, EventArgs e)
{
using (var db = new CosmeticContext())
{
bool verify = false;
Employee employee = db.Employee.FirstOrDefault(o => o.userName == UserNameBox.Text);
if (employee == null)
MessageBox.Show("Sads lietotajs nav atrasts datubaze", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
{
verify = PasswordAlgorithm.DoesPasswordMatch(employee.password, PasswordBox.Text);
if (verify)
if (employee.EmployeeType == EmployeeType.Seller)
{
CallCenter form1 = new CallCenter(employee);
form1.Show();
this.Hide();
}
}
}
}