0

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

城市表

CityId
CityName

员工表

 EmployeeId
 EmployeeName
 RegistrationDate
 FK_CityID

我正在尝试在 Employee 中插入数据,所有数据都插入完美,但FK_CityID 是 NULL

插入按钮单击代码 DateTime dt = new DateTime(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.City 中,我正在传递整个对象,即city1(城市对象)并且它正在完美传递,但在数据库中它将保存 NULL

我的服务类是

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

我没有得到我错的地方......

4

1 回答 1

0

嗨,我得到了解决方案..

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);
于 2012-04-17T10:03:07.667 回答