I have two Model classes: Attendance and Employee. I have defined the Employee class as:
public class Employee
{
public int Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
Then I have defined the Attendance class as:
public class Attendance
{
public int Id { get; set; }
public Employee Employee { get; set; } //This is the foreign key
public DateTime LoginDate { get; set; }
public DateTime LogoutDate { get; set; }
}
When I am trying to insert data into Employee table it works fine, but when I try to insert data inside Attendance table it shows an exception. I am checking the Employee properly and inserting only one row of Employee inside the Attendance table.
Here is an image of the exception:

In order to resolve the error you see (and get more details on the root problem) add a field for the EmployeeId to the Attendance class like so
The real problem (I believe) is that EF can’t determine the owner of the relationship. Without more information, it can’t decide if the Attendance to Employee relationship is many to one or one to one. A simple solution (I’m assuming it’s a many to one relationship) would be to add a collection of Attendance objects to the Employee class like so