An employee may have a department to which he belongs. A department has employees who belong to it, and there may be an employee who is the head of this department.
public class Employee
{
public string Id { get; set; } = null!;
public int? DepartmentId { get; set; }
public virtual Department? Department { get; set; }
}
public class Department
{
public int Id { get; set; }
public virtual ObservableCollection<Employee> Employees { get; set; } = new();
public string? DepartmentHeadId { get; set; }
public virtual Employee? DepartmentHead { get; set; }
}
public class DepartmentMap : IEntityTypeConfiguration<Department>
{
public void Configure(EntityTypeBuilder<Department> builder)
{
...
builder.HasMany(d => d.Employees).WithOne(e => e.Department).HasForeignKey(e => e.DepartmentId);
builder.HasOne(d => d.DepartmentHead).WithOne().HasForeignKey<Department>(d => d.DepartmentHeadId);
}
}
Application starts but in the logs it writes:
dbug: `CoreEventId.MultipleNavigationProperties`[10607] (Microsoft.EntityFrameworkCore.Model)
No relationship from 'Department' to 'Employee' has been configured by convention because there are multiple properties on one entity type - {'DepartmentHead', 'Employees'} that could be matched with the properties on the other entity type - {'Department'}. This message can be disregarded if explicit configuration has been specified in 'OnModelCreating'.
dbug: `CoreEventId.MultipleNavigationProperties`[10607] (Microsoft.EntityFrameworkCore.Model)
No relationship from 'Employee' to 'Department' has been configured by convention because there are multiple properties on one entity type - {'Department'} that could be matched with the properties on the other entity type - {'DepartmentHead', 'Employees'}. This message can be disregarded if explicit configuration has been specified in 'OnModelCreating'.
dbug: `CoreEventId.MultipleNavigationProperties`[10607] (Microsoft.EntityFrameworkCore.Model)
No relationship from 'Employee' to 'Department' has been configured by convention because there are multiple properties on one entity type - {'Department'} that could be matched with the properties on the other entity type - {'DepartmentHead', 'Employees'}. This message can be disregarded if explicit configuration has been specified in 'OnModelCreating'.
dbug: `CoreEventId.MultipleNavigationProperties`[10607] (Microsoft.EntityFrameworkCore.Model)
No relationship from 'Department' to 'Employee' has been configured by convention because there are multiple properties on one entity type - {'DepartmentHead', 'Employees'} that could be matched with the properties on the other entity type - {'Department'}. This message can be disregarded if explicit configuration has been specified in 'OnModelCreating'.
dbug: `CoreEventId.MultipleNavigationProperties`[10607] (Microsoft.EntityFrameworkCore.Model)
No relationship from 'Employee' to 'Department' has been configured by convention because there are multiple properties on one entity type - {'Department'} that could be matched with the properties on the other entity type - {'DepartmentHead', 'Employees'}. This message can be disregarded if explicit configuration has been specified in 'OnModelCreating'.
New contributor
Stormhead is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1