我有两节课
public class Project
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public int ManagerID { get; set; }
public int CoordID { get; set; }
[ForeignKey("ManagerID")]
public virtual Employee Manager { get; set; }
[ForeignKey("CoordID")]
public virtual Employee Coord { get; set; }
}
public class Employee
{
[Key]
public int EmpID { get; set; }
public string Name { get; set; }
[InverseProperty("ManagerID")]
public virtual ICollection<Project> ManagerProjects { get; set; }
[InverseProperty("CoordID")]
public virtual ICollection<Project> CoordProjects { get; set; }
}ManagerID和CoordID映射到Employee表的EmpID列。因为EF不能正确映射,所以我一直收到无效列的错误。我认为它正在寻找错误的列。
发布于 2013-01-29 05:36:48
我认为InverseProperty是用来指相关的导航属性,而不是外键。
public class Employee
{
[Key]
public int EmpID { get; set; }
public int Name { get; set; }
[InverseProperty("Manager")]
public virtual ICollection<Project> ManagerProjects { get; set; }
[InverseProperty("Coord")]
public virtual ICollection<Project> CoordProjects { get; set; }
}还有,为什么你的名字是整数而不是字符串?
发布于 2013-01-29 05:32:34
最好的猜测是通过OnModelCreating在您的上下文中使用fluent API。通过重命名列,EF无法找出要映射的原始对象,因此它被搞糊涂了。但是,Fluent API允许您使用类似于以下内容的内容手动指定地图:
public class MyContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
public DbSet<Project> Projects { get; set; }
protected override OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Project>()
.HasRequired(x => x.Manager)
.WithMany(x => x.ManagerProjects)
.HasForeignKey(x => x.ManagerID);
modelBuilder.Entity<Project>()
.HasRequired(x => x.Coord)
.WithMany(x => x.CoordProjects)
.HasForeignKey(x => x.CoordID);
}
}https://stackoverflow.com/questions/14571267
复制相似问题