I have a modelling question that I need help understanding.
I have a table called Employee and a table called Office. There is a 1 to many relationship between the classes (1 employee has one office and one office has multiple employees).
The goal is to create classes from those tables and I have found two ways:
1:
public class Employee
{
private string Name { get; set; }
private Office Office { get; set; }
}
public class Office
{
private string OfficeName { get; set; }
}
2:
public class Employee
{
private string Name {get; set;}
}
public class Office
{
private string OfficeName { get; set; }
private List<Employee>Employees { get; set; }
}
Which way is the correct way to model this ?
I find the second approach to be easier to work with (more natural). Reason being, when you model your data this way:
You can create these tables (or use entity framework to create the tables for you) in the following manner:
The
one-to-manyrelationship is represented via theFKin theemployeetable and theemployeehas aFKto the office where he/she belongs. Then by using simplejoinsyou can get all info you need from the tables. e.g.Get all
employeesfor anofficeThis would've been a bit more complicated if you had used your first approach.