EntityFramework : Invalid column name *_ID1
Solution 1
Hi After spending some time I could fix this problem by using ForeignKey
attribute on public virtual Department Department { get; set; }
property of Employee
class.
Please see below code.
[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; }
}
This fixed my problem. Are there any other solution to fix this? Using fluent API?
Solution 2
I've also gotten this problem in my EF
one-many
deals where the one
has a List
of the many
property and my mapping didn't specify that property. For example take:
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; }
}
Then in my mapping, the way that caused the Exception
(the incorrect way):
builder.HasOne(x => x.Notification).WithMany()
.HasForeignKey(x => x.NotificationID);
What fixed it (the correct way) was specifying the WithMany
property:
builder.HasOne(x => x.Notification).WithMany(x => x.Recipients)
.HasForeignKey(x => x.NotificationID);
Solution 3
For me, the issue was resolved by removing a (duplicate?) virtual property.
Using the OP's example:
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; }
}
Turns into:
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; }
}
Solution 4
I had the same error, my issue was the FK was a long
but I had it as an int
in the model. EF generated a new column because it didn't match types on the FK so it assumed they weren't the same and went ahead with making another one but putting 1 at the end because there was already one with the proper name. Making sure the types matched resolved the issue for me.
Solution 5
In my case I added a virtual property on top of the auto generated property I fixed it by adding the NotMapped attribute to my property, or you could configure with fluent api
public partial class Control
{
[NotMapped]
public virtual ICollection<Control> Children { get => this.InverseParent; set => this.InverseParent = value; }
}
Related videos on Youtube
user2243747
Updated on October 18, 2022Comments
-
user2243747 over 1 year
I am trying to implement DbContext for couple of tables called '
Employee
' and 'Department
' Relationship between Employee and Department is many to one. i.e. department can have many employees.Below are the EntityFramework classes I designed ( CodeFirst approach )
[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; } }
While adding Employee record I am getting below exception
"Invalid column name 'Department_ID1'."
I am not sure why EF is referring to
Department_ID1
. Do I need to add configuration inOnModelCreating
method ofDbContext
?I am using EF version
6.1.1
-
Tawab Wakil over 2 years+1 This solution can be easy to overlook, but in cases where VS generates the context for you it sometimes decides to create property names that don't match the original names. You then end up with a duplicate when you add it manually thinking it's missing.