I have two entities:
The Audit class:
public class Audit
{
public string AuditId{get;set;}
public int EmployeeId{get;set;}
public virtual ModEmployee{get;set;}
}
The Employee class:
public class Employee
{
public int EmployeeId{get;set}
}
While loading Set<Audit>, I want the Employee property of the Audit class to be populated.
This is clearly a case of One to Many relation, where one Audit will have one Employee but an Employee can be in several Audits.
My Fluent API looks like below:
protected override OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Audit>()
.HasOne(a=>a.ModEmployee)
.WithMany()
.HasForeignKey("EmployeeId");//a=>a.EmployeeId wont work.
}
I have been searching for a solution for sometime and took some inputs from this answer but its for One-To-One. Then this resource shows a way but that will require me to have a collection of Audits in the Employee class.
I cant change the existing models or add annotations as they are already being used by some other entity framework version (not core).
Presently I am getting the following error:
System.InvalidOperationException: 'Unable to determine the relationship represented by navigation property 'Employee.ModEmployee' of type 'Employee'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.'
If I add a ignore method on the builder of Audit the error goes away, like so: .Ignore(a=>a.ModEmployee) but I won't get the entity object inside Audit. Any help will be appreciated. I am on EFCore 3.1.10. Thanks for reading.
Any help will be appreciated.