【问题标题】:EntityFramework : Invalid column name *_ID1实体框架:无效的列名 *_ID 1
【发布时间】:2016-06-03 05:28:58
【问题描述】:

我正在尝试为名为“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。是否需要在DbContextOnModelCreating方法中添加配置?

我用的是EF版6.1.1

【问题讨论】:

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


    【解决方案1】:

    嗨,经过一段时间后,我可以通过在 public virtual Department Department { get; set; } 类的 public virtual Department Department { get; set; } 属性上使用 ForeignKey 属性来解决此问题。

    请看下面的代码。

     [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?

    【讨论】:

      【解决方案2】:

      我在我的EFone-many 交易中也遇到了这个问题,其中one 具有many 属性的List,而我的映射没有指定该属性。例如采取:

      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 属性:

      builder.HasOne(x => x.Notification).WithMany(x => x.Recipients)
          .HasForeignKey(x => x.NotificationID);
      

      【讨论】:

        【解决方案3】:

        对我来说,通过删除(重复?)虚拟属性解决了这个问题。

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

        【讨论】:

        • +1 这个解决方案很容易被忽略,但是在 VS 为您生成上下文的情况下,它有时会决定创建与原始名称不匹配的属性名称。然后,当您手动添加它时,您会认为它丢失了。
        【解决方案4】:

        在我的例子中,我在自动生成的属性之上添加了一个虚拟属性 我通过将 NotMapped 属性添加到我的属性来修复它,或者您可以使用 fluent api 进行配置

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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:

            只需在虚拟属性上添加[NotMapped] 注释即可解决此问题。

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

            在你的模型构建器中:

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

            如果你想按部门打电话,只需翻转这个。

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

            【讨论】:

              猜你喜欢
              • 2013-11-26
              • 2018-05-07
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-08-28
              • 1970-01-01
              相关资源
              最近更新 更多