2 years ago
#65463
baouss
Multiple Reference Navigation properties to same table
In a EF Core model there are two classes, Company and Employee. 1 Company can have multiple Employees. I am wondering if I can add to reference navigation properties (collection of employees with different roles, i.e. manager role) referencing the same employees table and have EF Core figure out which employees to display in what property, depending on a property of the employee class (isManager, true/false).
See below, would this be possible?
public class Company
{
public int Id { get; set; }
public string Name { get; set; } = string.empty;
public virtual ICollection<Employee> Minions { get; set; }
public virtual ICollection<Employee> Managers { get; set; }
public Company()
{
Minions = new HashSet<Employee>();
Managers = new HashSet<Employee>();
}
}
public class Employee
{
public int Id { get; set; }
public string Name { get; set; } = string.empty;
public int CompanyId { get; set; }
public virtual Company Company { get; set; } = null!;
public bool IsManager { get; set; }
}
Id | Name |
---|---|
1 | "MonkeyBizz Inc." |
Id | Name | CompanyId | IsManager |
---|---|---|---|
1 | "John Doe" | 1 | 0 |
2 | "Jane Doe" | 1 | 0 |
3 | "Skippy the Magnificent" | 1 | 1 |
{
"id": 1,
"name": "MonkeyBizz Inc.",
"minions": [
{ "id": 1, "name": "John Doe", "companyId": 1, "isManager": false },
{ "id": 2, "name": "Jane Doe", "companyId": 1, "isManager": false }
],
"managers": [
{ "id": 3, "name": "Skippy the Magnificent", "companyId": 1, "isManager": true }
]
}
entity-framework-core
relational-database
navigation-properties
0 Answers
Your Answer