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

entity-framework

Question

I got the message ERROR: OrganizationStructure ID is an invalid column name.

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

ACTION INDEX:

    return View(_organizationStructureRepository.GetAll().ToList() 
             .Where(t => t.ParentID == null));
1
36
1/25/2012 3:55:45 AM

Accepted Answer

You failed to connect your FK property with a navigation property, which is why. I predict theParentID should refer to the parentsOrganizationStructure and ChildrenItems should highlight youngstersOranizationStructures .

If your model is missingParent parent navigation propertyOrganizationStructure You must instruct EF using fluent-API.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

I've discovered that when an ICollection mentions a table and there isn't a column it can identify, it makes one for you to attempt to link the tables. This notably occurs with ICollection, and attempting to figure it out has drove me "batty."



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