Entity Framework - Invalid Column Name '*_Id"

C# Entity Framework - Invalid column name

To solve the "Invalid column name 'BrandBike_BrandID'" error, you need to add a column named BrandId to your table Bike. This Column should store the BrandId that the Bike is related to.

I do not have your Bike table structure, but the "Invalid column name 'Strokes'" indicates that there is no column named Strokes in that table.

Entity Framework: Invalid column name 'OrganizationStructure_ID'

That is because you didn't pair your FK property with a navigation property. I expect the ParentID should point to parent OrganizationStructure and ChildrenItems should point to children OranizationStructures.

If your model doesn't contain Parent navigation property to parent OrganizationStructure you must use fluent-API to tell EF that ParentID is FK:

modelBuilder.Entity<OrganizationStructure>()
.HasMany(o => o.ChildrenItems)
.WithOptional()
.HasForeignKey(c => c.ParentID);

EntityFramework : Invalid column name *_ID1

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?

entity framework Invalid column name Id (two foreign keys from the same primary table)

You need 2 navigation properties because you have 2 foreign keys :

public class Resource
{
public int Id { get; set; }

public DateTime CreationDate { get; set; }
public DateTime UpdateDate { get; set; }

public virtual ICollection<Task> ApproverTasks { get; set; }
public virtual ICollection<Task> WorkingTasks { get; set; }
}

In DbContext you specify them in WithMany:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Task>()
.HasRequired(t => t.WorkingResource)
.WithMany(r => r.WorkingTasks)
.HasForeignKey(t => t.WorkingResourceId)
.WillCascadeOnDelete(false);

modelBuilder.Entity<Task>()
.HasRequired(t => t.ApproverResource)
.WithMany(r => r.ApproverTasks)
.HasForeignKey(t => t.ApproverResourceId)
.WillCascadeOnDelete(false);

modelBuilder.Entity<ExternalResource>()
.ToTable("ExternalResources");
}

You need to explicit tell table name for ExternalResources because of Table-Per-Type inheritance.

C# code-first EF 6. Invalid Column Name

So, I don't know why but, when a send to my GridView as a List<T> model it gives me that error, but if I send using AsEnumarable instead of ToList, it works:

public ActionResult IndexContratos(int id)
{
var anexos = db.Anexos
.Include(a => a.Contrato)
.Include(a => a.Pessoa)
.Include(a => a.TipoDocumento)
.Where(x => x.ContratoId == id);

AnexosContratoTemps(id);

return View("Grid", anexos.AsEnumerable());//Works !!
//return View("Grid", anexos.ToList()); //Invalid column name...
}

My View model is:

@model IEnumerable<Anexos> 

But a List<T> is a IEnumarable<T>, I really don't understand that why ToList gives me that error, can someone explain?

Edit

Finally(by coincidence) I found what was that bug, I forgot to say that the Anexos property in MyContext class was virtual, something like this:

public DbSet<Anexos> Anexos { get; set; } //WRONG!
public virtual DbSet<Anexos> Anexos { get; set; }//YAYYY!!

System.Data.SqlClient.SqlException: Invalid column name 'Gender_id'

Genders_Id and Programs_Id are the default EF6 conventional names of FK columns for the one to many relationships introduced respectively by the Genders.Students and Programs.Students collection navigation properties.

Since your FK property / column names are not conventional, you should specify them by either [ForeinKey] data annotations:

public class Programs
{
// ...
[ForeignKey("Program")]
public List<Students> Students { get; set; }
}

public class Genders
{
// ...
[ForeignKey("Gender")]
public List<Students> Students { get; set; }
}

or (my preferred) fluent API:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// ...

modelBuilder.Entity<Genders>()
.HasMany(e => e.Students)
.WithRequired()
.HasForeignKey(e => e.Gender);

modelBuilder.Entity<Programs>()
.HasMany(e => e.Students)
.WithRequired()
.HasForeignKey(e => e.Program);
}


Related Topics



Leave a reply



Submit