Shahnawaz
Shahnawaz

Reputation: 646

Foreign Key help in Entity Framework Codefirst

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:

enter image description here

Upvotes: 1

Views: 917

Answers (4)

SynerCoder
SynerCoder

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

Michael Brown
Michael Brown

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

jfin3204
jfin3204

Reputation: 709

Try placing a key attribute on your employee entity.

Upvotes: 0

BenjaminPaul
BenjaminPaul

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

Related Questions