Entity Framework: Invalid column name 'OrganizationStructure_ID'

What I have figured out is when you have an ICollection that references a table and there is no column that it can figure out, it creates one for you to try to make the connection between the tables. This specifically happens with ICollection and has driven me "batty" trying to figure it out.


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);

It's also could be if you declare reference field in child entity as simple field, but not a property!

int ParentId  //will be ignored; 

int ParentId {get; set;} // it'ok (but could be ignored 
                         //if the parent entity name isn't 'Parent'); 

[ForeignKey("MyParentEntity")] 
int ParentId {get; set;} // is the best way (or use fluent-api)

I had a similar issue, removing unwanted entry of public virtual ICollection, solved it.