C# 实体框架Codefirst中的外键帮助

C# 实体框架Codefirst中的外键帮助,c#,.net,entity-framework,ef-code-first,C#,.net,Entity Framework,Ef Code First,我有两个示范班:出勤和员工。我已将Employee类定义为: public class Employee { public int Id { get; set; } public string Username { get; set; } public string Password { get; set; } } public class Attendance { public int Id { get; set; } public Employee

我有两个示范班:出勤和员工。我已将Employee类定义为:

public class Employee
{
    public int Id { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
}
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; }
}
然后我将出勤班定义为:

public class Employee
{
    public int Id { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
}
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; }
}
当我尝试将数据插入Employee表时,它工作正常,但当我尝试将数据插入考勤表时,它会显示一个异常。我正在正确检查员工,并在考勤表中只插入一行员工

以下是异常的图像:


您需要公开密钥本身,而不仅仅是实体

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

您需要公开密钥本身,而不仅仅是实体

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

尝试在员工实体上放置键属性

尝试在员工实体上放置一个键属性

为了解决您看到的错误并获得有关根本问题的更多详细信息,请向考勤类添加EmployeeId字段,如下所示

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; }
}
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;}
}
我认为真正的问题是EF无法确定关系的所有者。如果没有更多的信息,它无法决定考勤与员工的关系是多对一还是一对一。我假设这是一种多对一关系,一个简单的解决方案是向Employee类中添加一组Attention对象,如下所示

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

为了解决您看到的错误并获得有关根本问题的更多详细信息,请向考勤类添加EmployeeId字段,如下所示

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; }
}
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;}
}
我认为真正的问题是EF无法确定关系的所有者。如果没有更多的信息,它无法决定考勤与员工的关系是多对一还是一对一。我假设这是一种多对一关系,一个简单的解决方案是向Employee类中添加一组Attention对象,如下所示

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

您需要定义外键属性:

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; }
}
将外键添加为int后,可以对其进行配置:

public class AttendanceConfiguration : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<Attendance>
{
    public AttendanceConfiguration()
    {
        this.HasRequired(a => a.Employee)
            .WithMany()
            .HasForeignKey(a => a.EmployeeID);
    }
}
更新
通过使用没有参数的WithMany重载,您可以建立一个单向的一对多关系。

您需要定义外键属性:

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; }
}
将外键添加为int后,可以对其进行配置:

public class AttendanceConfiguration : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<Attendance>
{
    public AttendanceConfiguration()
    {
        this.HasRequired(a => a.Employee)
            .WithMany()
            .HasForeignKey(a => a.EmployeeID);
    }
}
更新
通过使用没有参数的WithMany重载,您可以建立一个单向的一对多关系。

根据主异常的说明,内部异常是什么。另外,您如何创建和保存考勤对象?显示示例。根据主异常的说明,内部异常是什么。另外,您如何创建和保存考勤对象?举个例子。我不是EF方面的专家,但出席人数收集不需要是虚拟的吗?好的,我会试试这个,但这是一个简单的一对一关系。如果是一对一,我该怎么做呢?出席不需要是虚拟的,看看这个,更多关于虚拟的答案&EF如果你使用延迟加载,我相信这是EF的默认值,它确实需要是虚拟的。否则,你是对的,没有它也行。我不是EF方面的专家,但出席人数收集不需要是虚拟的吗?好的,我会试试这个,但这是一个简单的一对一关系。如果是一对一,我该怎么做呢?出席不需要是虚拟的,看看这个,更多关于虚拟的答案&EF如果你使用延迟加载,我相信这是EF的默认值,它确实需要是虚拟的。否则,你是对的,不用它也行。