Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

INDEX ACTION:

    return View(_organizationStructureRepository.GetAll().ToList()               .Where(t => t.ParentID == null)); 
like image 635
Dorjsuren Ochir Avatar asked Jan 25 '12 03:01

Dorjsuren Ochir


1 Answers

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); 
like image 192
Ladislav Mrnka Avatar answered Sep 21 '22 19:09

Ladislav Mrnka