一对多关系将外键保存为 NULL asp.net

本文关键字:NULL asp net 保存 关系 一对多 | 更新日期: 2023-09-27 18:31:27

我正在尝试在 .net 3.5 的实体框架中定义我的两个表之间的一对多关系。

城市表

CityId
CityName

员工表

 EmployeeId
 EmployeeName
 RegistrationDate
 FK_CityID

我正在尝试在员工中插入数据,所有数据都完美插入FK_CityID但被输入为空

插入按钮单击代码 日期时间 dt = 新的日期时间(2008, 12, 12);

    EmployeeService es = new EmployeeService();
    CityService cs = new CityService();
    Payroll.Entities.Employee e1 = new Payroll.Entities.Employee();
    Payroll.Entities.City city1 = cs.SelectCity(Convert.ToInt32(cmbCity.SelectedItem.Value));
    e1.FK_CityID = city1;
    e1 = Payroll.Entities.Employee.CreateEmployee(0, "Archana",dt);
    es.AddEmpoyee(e1);

e1 中。城市我正在传递整个对象,即 city1(城市对象),它正在完美传递,但在数据库中它将保存 NULL

我的服务类是

public string AddEmpoyee(Payroll.Entities.Employee e1)
        {
            //DAO
            Payroll_DAO payrollDAO = new Payroll_DAO();
            payrollDAO.AddToEmployee(e1);
            payrollDAO.SaveChanges();
            return "SUCCESS";
        }

我没有明白我错的地方...

一对多关系将外键保存为 NULL asp.net

嗨,

我得到了解决方案..

Payroll.Entities.City city1 = new Payroll.Entities.City();
city1 = cs.SelectCity(Convert.ToInt64(cmbCity.SelectedItem.Value));
e1.EmployeeName = "Archana";
e1.RegistrationDate= dt;
e1.FK_CityID = city1;
es.AddEmpoyee(e1);