'OrganizationStructure ID' is an invalid column name in Entity Framework.

entity-framework

Question

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));
1
36
1/25/2012 3:55:45 AM

Accepted Answer

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);
43
1/25/2012 9:49:00 AM

Popular Answer

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.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow