1

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

1
  • What is the inner exception (as per the main exception's instructions). Also how are you creating and saving an Attendance object? (Show example). Commented Dec 4, 2012 at 15:02

4 Answers 4

2

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;} } 
Sign up to request clarification or add additional context in comments.

4 Comments

I'm no expert on EF, but doesn't that Attendances collection need to be virtual?
Ok I will try this, but this is a simple one to one relationship. How can I do it if its one to one?
Attendances doesnt need to be virtual, see this SO answer for more on virtual & EF stackoverflow.com/a/7738919/637425
If you're using lazy loading (which I believe is the default for EF), it does need to be virtual. Otherwise, you're right it works fine without.
2

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.

Comments

0

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; } } 

Comments

0

Try placing a key attribute on your employee entity.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.