我得到:错误:无效的列名'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; }

指标 Action :
    return View(_organizationStructureRepository.GetAll().ToList()
             .Where(t => t.ParentID == null));

最佳答案

那是因为您没有将FK属性与导航属性配对。我希望ParentID应该指向父类OrganizationStructure,而ChildrenItems应该指向子类OranizationStructures

如果您的模型不包含父ParentOrganizationStructure导航属性,则必须使用fluent-API告诉EF ParentID是FK:

modelBuilder.Entity<OrganizationStructure>()
            .HasMany(o => o.ChildrenItems)
            .WithOptional()
            .HasForeignKey(c => c.ParentID);

关于entity-framework - Entity Framework :无效的列名 'OrganizationStructure_ID',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8997562/

10-11 02:04