定义属性时导航属性错误

时间:2018-11-16 13:52:32

标签: c# asp.net-mvc navigation-properties

我的错误是

  

指定的包含路径无效。 EntityType'Database.Agency'未声明名称为'Sectors'的导航属性。

当我确定自己定义正确时

代理商模型

public partial class Agency
{
    public Agency()
    {
        this.Sectors = new HashSet<Sector>();
    }

    public int id { get; set; }
    public bool deleted { get; set; }
    public string name { get; set; }
    public string address1 { get; set; }
    public string address2 { get; set; }
    public string address3 { get; set; }
    public string town { get; set; }
    public int countyid { get; set; }
    public string postcode { get; set; }
    public string telephoneno { get; set; }
    public string websiteurl { get; set; }
    public string companyno { get; set; }
    public string vatno { get; set; }

    public virtual ICollection<Sector> Sectors { get; set; }
}

部门模型

public partial class Sector
{
    public Sector()
    {
        this.Agencies = new HashSet<Agency>();
    }

    public int id { get; set; }
    public string name { get; set; }

    public virtual ICollection<Agency> Agencies { get; set; }

}

这是一个“多对多”的关系。

这是发生错误的地方

[HttpGet]
    public ActionResult AddOrEdit(int? id)
    {
        if (id == 0)
        {
            return View(new AgencyAll());
        }
        Agency agency = _db.Agencies
            .Include(p => p.Sectors)
            .Single(i => i.id == id);

        if (agency == null)
        {
            return HttpNotFound();
        }

        ViewBag.CountyList = new SelectList(GetCountyList(), "Value", "Text");
        PopulateAssignedBatData(agency);

        return View(new AgencyAll {Agency = _db.Agencies.FirstOrDefault(x => x.id == id)});
    }

特别是

Agency agency = _db.Agencies
            .Include(p => p.Sectors)
            .Single(i => i.id == id);

情报部门正在寻找具有该属性的代理商,我曾尝试在Visual Studio中的edmx上定义导航属性,但是每次这样做,它都会删除所有模型,因此我需要手动定义该导航属性不知道我在这里想念的是什么。非常感谢您的帮助。

0 个答案:

没有答案