Я предполагаю, что вы используете Entity Framework с учетом кода, который вы предоставляете.Вы должны установить отношения между двумя вашими сущностями и позволить EF обработать это для вас:
public class Employee {
public int EmployeeId { get; set; }
public virtual Address Address { get; set; }
}
public class Address {
public int AddressId { get; set; }
public int EmployeeId { get; set; }
public virtual Employee Employee { get; set; }
}
Теперь, когда вы создаете сущности:
// create a new Employee
Employee employee = new Employee();
// create a new Address
Address address = new Address();
// associate the address with the new employee
employee.Address = address;
// add the employee to the data context
db.employee.AddObject(employee);
// when you call save changes, since your Address is attached to your
// employee, it will get added for you and you don't have to add it to the
// context yourself. Entity Framework will save the Employee, get the ID
// from this table and then add a new Address record using the ID that was
// just inserted.
db.SaveChanges();
Это добавит оба объекта и добавитвнешний ключ для вас.
Редактировать
Это первый пример кода.Если вы сначала используете базу данных с помощью дизайнера, просто установите отношения с помощью дизайнера.Код для добавления сотрудника в этом случае не должен изменяться.