Reputation: 646
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:
Upvotes: 1
Views: 917
Reputation: 12776
You need to define a foreign key property:
public class Attendance
{
public int Id { get; set; }
public int EmployeeID { get; set; }
public Employee Employee { get; set; }
public DateTime LoginDate { get; set; }
public DateTime LogoutDate { get; set; }
}
public class Employee
{
public int Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
After adding the foreign key as an int you can configure it:
public class AttendanceConfiguration : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<Attendance>
{
public AttendanceConfiguration()
{
this.HasRequired(a => a.Employee)
.WithMany()
.HasForeignKey(a => a.EmployeeID);
}
}
And then in the context define this Configuration
public class Context : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new AttendanceConfiguration());
}
}
UPDATE
By using the WithMany() overload which has no parameters, you can make a single unidirectional one to many relationship.
Upvotes: 2
Reputation: 9153
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
public class Attendance
{
public int Id { get; set; }
//This exposes the foreign key on attendance
public int EmployeeId {get; set;}
public Employee Employee { get; set; } //This is the foreign key
public DateTime LoginDate { get; set; }
public DateTime LogoutDate { get; set; }
}
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
public class Employee
{
public int Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public virtual ICollection<Attendance> Attendances {get; protected set;}
}
Upvotes: 2
Reputation: 2931
You need to expose the key itself, not just the Entity.
public class Attendance
{
public int Id { get; set; }
public Employee Employee { get; set; }
public int EmployeeId { get; set; } // THIS is the foreign key.
public DateTime LoginDate { get; set; }
public DateTime LogoutDate { get; set; }
}
Upvotes: 0