0

I am trying to define a one-to-many relationship between my two table in Entity Framework in .net 3.5.

City Table

CityId
CityName

Employee Table

 EmployeeId
 EmployeeName
 RegistrationDate
 FK_CityID

I am trying to insert Data in Employee all data inserted Perfectly but FK_CityID is instred NULL.

Insert Button click Code 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);

In e1.City I am Passing whole object that is city1(object of city) and it is passing Perfectly but In database it will save NULL

MyService Class is

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

I am not getting where I am wrong...

Smily
  • 2,646
  • 4
  • 23
  • 31
  • 1
    Open up your SQL Profiler and see, What are the values getting passed, when you save!. Or put a put a break point at es.AddEmployee(e1) and see the fields of e1. – Hari Gillala Apr 16 '12 at 13:27
  • I put a break point at **es.AddEmployee(e1)**.. I am not getting value in **FK_CityID**. but insted of that i am getting **cityReference** – Smily Apr 17 '12 at 05:41

1 Answers1

0

Hi I got the solution..

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);
Smily
  • 2,646
  • 4
  • 23
  • 31