Entity Framework: Invalid column name 'OrganizationStructure_ID'

I get : ERROR: Invalid column name 'OrganizationStructure_ID'.

    public OrganizationStructure()
        ChildrenItems = new HashSet<OrganizationStructure>();
        InputDate = DateTime.Now;

    public int ID { get; set; }
    public string Name { get; set; }

    public virtual int? ParentID { get; set; }
    public int OrganizationID { get; set; }
    public int OrganizationTypeID { get; set; }
    public int OrganizationActivityID { get; set; }
    public int OrganizationLocationID { get; set; }

    public string AddRemark { get; set; }
    public int UserId { get; set; }
    public DateTime InputDate { get; set; }
    public int? RemAttr { get; set; }

    public virtual ICollection<OrganizationStructure> ChildrenItems { get; set; }


    return View(_organizationStructureRepository.GetAll().ToList() 
             .Where(t => t.ParentID == null));


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:

            .HasMany(o => o.ChildrenItems)
            .HasForeignKey(c => c.ParentID);

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.

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

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

int ParentId {get; set;} // is the best way (or use fluent-api)

If this is a test project, make sure the connection strings are updated for test project

I upgraded the project to newer version of .Net Framework, so I suppose there was a conflict with the Entity Framework libraries, uninstall/install Entity Framework worked for me

uninstall-package entityframework
install-package entityframework

Need Your Help

How to convert unix timestamp to date in Spark

scala datetime apache-spark timestamp nscala-time

I have a data frame with a column of unix timestamp(eg.1435655706000), and I want to convert it to data with format 'yyyy-MM-DD', I've tried nscala-time but it doesn't work.

OData $filter with multiple predicates

wcf filter odata predicate

If I have two entities in my model, "People" and "Addresses", and a particular Person has zero or more addresses, accessed via an AddressList navigation property, can I write an OData query that an...