EntityFramework:无效的列名* _ID1

时间:2016-06-03 05:28:58

标签: c#-4.0 entity-framework-6

我正在尝试为名为“Employee”和“Department”的几个表实现DbContext 员工与部门之间的关系是多对一的。即部门可以有很多员工。

以下是我设计的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的{​​{1}}方法添加配置?

我使用的是EF版DbContext

6 个答案:

答案 0 :(得分:11)

嗨花了一些时间后,我可以使用ForeignKey类的public virtual Department Department { get; set; }属性上的Employee属性解决此问题。

请参阅以下代码。

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

    [ForeignKey("Department_ID")]
    public virtual Department Department { get; set; }
}

这解决了我的问题。有没有其他解决方案来解决这个问题?使用流畅的API?

答案 1 :(得分:5)

我也在我的EF one-many个交易中遇到了这个问题,其中one的{​​{1}}属性为List而我的映射没有' t指定该属性。例如:

many

然后

public class Notification
{
    public long ID { get; set; }     

    public IList<NotificationRecipient> Recipients { get; set; }
}

然后在我的映射中,导致public class NotificationRecipient { public long ID { get; set; } public long NotificationID { get; set; } public Notification Notification { get; set; } } 不正确的方式)的方式:

Exception

修正了它(正确的方式)指定了builder.HasOne(x => x.Notification).WithMany() .HasForeignKey(x => x.NotificationID); 属性:

WithMany

答案 2 :(得分:0)

只需在虚拟属性上放置[NotMapped]批注即可解决此问题。

public class Employee
{
    [ForeignKey("Department")]
    public int Department_ID
    [NotMapped]
    public virtual Department Department { get; set; }
}

在您的modelBuilder中:

    modelBuilder.Entity<Employee>(entity =>
        {
            entity.HasOne(e => e.Department);
        });

如果要按部门致电,只需翻转一下即可。

我们使用[NotMapped]批注,以便EF Core在查看您的数据库时将忽略它。

答案 3 :(得分:0)

对我来说,该问题已通过删除(重复的)虚拟属性解决。

使用OP的示例:

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Department_ID { get; set; }
    public virtual Department Department { get; set; }
}

public class Department
{
    public int ID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Employee>  Employees { get; set; }
}

变成:

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Department_ID { get; set; }
}

public class Department
{
    public int ID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Employee>  Employees { get; set; }
}

答案 4 :(得分:0)

就我而言,我在自动生成的属性之上添加了一个虚拟属性 我通过在属性中添加NotMapped属性来解决此问题,或者可以使用流利的api配置

    public partial class Control
    {
        [NotMapped]
        public virtual ICollection<Control> Children { get => this.InverseParent; set => this.InverseParent = value; }
    }

答案 5 :(得分:0)

我遇到了同样的错误,我的问题是 FK 是 long,但我在模型中将它作为 int。 EF 生成了一个新列,因为它与 FK 上的类型不匹配,因此它假设它们不相同并继续制作另一个列,但在末尾放置 1,因为已经有一个具有正确名称的列。确保类型匹配为我解决了这个问题。