【问题标题】:A specified Include path is not valid. The EntityType does not declare a navigation property with the name指定的包含路径无效。 EntityType 未声明具有名称的导航属性
【发布时间】:2016-11-02 06:10:00
【问题描述】:

我在下面的代码有效。它会拉入我的 One Repository 中每个项目的列表。

当我添加第二个表以从该表中提取所有项目时,我收到以下错误,在我的 DataTwo 上,我无法弄清楚为什么它会抛出此错误,因为第一个表的编程方式完全相同。

“指定的包含路径无效。EntityType 未声明具有名称的导航属性”

查看模型

public IList<OneVM> Ones { get; set; }
public IList<TwoVM> Twos { get; set; }
public ViewModelVM()
{
    this.Ones = new List<OneVM>();
    this.Twos = new List<TwoVM>();
}

下面的工作原始代码(控制器)

public ActionResult Directory()
{
    var vm = new ViewModelVM();
    var datas = _OneRepository.GetData();
    vm.Datas = _mapper.Map<IList<DataVM>>(datas.OrderBy(i => i.Name)); 
    return View(vm);
}

以下所需的损坏代码(控制器)

    public ActionResult Directory()
    {
 var vm = new FormDirectoryVM();
            var datas = _OneRepository.GetData();
            var datasTwo= _TwoRepository.GetMoreData();
            vm.Datas = _mapper.Map<IList<DataVM>>(datas.OrderBy(i => i.Name)); 
        return View(vm);
            vm.DatasTwo= _mapper.Map<IList<DataTwoVM>>(datasTwo);
return View(vm);
}

【问题讨论】:

  • _OneRepository.GetData().AsEnumerable().Concat(_TwoRepository.GetMoreData());
  • 恐怕这会给我同样的错误。

标签: asp.net-mvc controller


【解决方案1】:

问题出在我的存储库上。我包括了一些不需要的东西。

 public IEnumerable<Two> GetMoreData()
        {
            return _context.Twos
                .Include(i => i.Title) // I don't need this line
                .Include(i => i.Description) // I don't need this line either
                .Include(i => i.Keywords)
                .Include(j => j.Text) // Or this Line
                .Where(i => !i.IsDeleted)
             ;
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多