我正在尝试为几个名为 ' 的表实现 DbContextEmployee
' and 'Department
'
员工和部门之间的关系是多对一的。即部门可以有很多员工。
下面是我设计的EntityFramework类(CodeFirst方法)
[Table("Employee")]
public class Employee
{
[DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Column("Name")]
public string Name { get; set; }
[Column("Department_ID")]
public int Department_ID { get; set; }
public virtual Department Department { get; set; }
}
[Table("Department")]
public class Department
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
[Column("Name")]
public string Name { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
添加员工记录时出现以下异常
"Invalid column name 'Department_ID1'."
我不确定为什么 EF 指的是Department_ID1
。是否需要添加配置OnModelCreating
的方法DbContext
?
我用的是EF版本6.1.1
我的也遇到了这个问题EF
one-many
交易的地方one
has a List
of the many
属性,而我的映射没有指定该属性。例如:
public class Notification
{
public long ID { get; set; }
public IList<NotificationRecipient> Recipients { get; set; }
}
then
public class NotificationRecipient
{
public long ID { get; set; }
public long NotificationID { get; set; }
public Notification Notification { get; set; }
}
然后在我的映射中,导致Exception
(the 不正确 way):
builder.HasOne(x => x.Notification).WithMany()
.HasForeignKey(x => x.NotificationID);
是什么解决了这个问题(correct方式)指定WithMany
财产:
builder.HasOne(x => x.Notification).WithMany(x => x.Recipients)
.HasForeignKey(x => x.NotificationID);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)