实体框架:无效的列名称“OrganizationStructure_ID” [英] Entity Framework: Invalid column name 'OrganizationStructure_ID'

查看:190
本文介绍了实体框架:无效的列名称“OrganizationStructure_ID”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到:错误:列名称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:

INDEX ACTION:

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


推荐答案

这是因为您没有配对您的FK属性具有导航属性。我希望 ParentID 应该指向父 OrganizationStructure ChildrenItems 应指向孩子 OranizationStructures

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.

如果您的模型不包含导航属性为父 OrganizationStructure 你必须使用流畅的API来告诉EF, ParentID 是FK:

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

这篇关于实体框架:无效的列名称“OrganizationStructure_ID”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆