2012-04-16 59 views
0

我試圖在.net 3.5中的實體框架中的我的兩個表之間定義一對多關係。一對多關係在asp.net中將外鍵保存爲NULL

市表

CityId 
CityName 

EMPLOYEE表

EmployeeId 
EmployeeName 
RegistrationDate 
FK_CityID 

我試圖在員工中插入數據插入完美的所有數據,但FK_CityID是instred 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

爲MyService類是

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

,我沒有得到我的錯......

+1

打開你的SQL事件探查器,看看,什麼是價值獲得通過,當您保存!或者在es.AddEmployee(e1)放一個斷點並查看e1的字段。 – 2012-04-16 13:27:10

+0

我在** es.AddEmployee(e1)**處放置了一箇中斷點**我沒有在** FK_CityID **中獲得價值。但由於我得到** cityReference ** – Smily 2012-04-17 05:41:02

回答

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); 
相關問題