EntityFramework.dll 中发生类型 'System.ArgumentException' 的异常,但未在用户代码中处理

An exception of type 'System.ArgumentException' occurred in EntityFramework.dll but was not handled in user code

我遇到了一个问题:

An exception of type 'System.ArgumentException' occurred in EntityFramework.dll but was not handled in user code

Additional information: The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.

我该如何解决?

这个问题发生在FindAll方法

articleViewModel.AttachmentFiles = AttachmentFileBLL.Instance.FindAll(c => c.ArticleId == articleViewModel.Id).ToList();

FindAll 方法:

public virtual IQueryable<TModel> FindAll(params Expression<Func<TModel, object>>[] includeProperties)
    {
        IQueryable<TModel> items = RepositoryContainer<TRepository>().FindAll();

        if (includeProperties != null)
        {
            foreach (var includeProperty in includeProperties)
            {
                items = items.Include(includeProperty); // Problem occurred here! 
            }
        }
        return items;
    }

public virtual int? ArticleId { get; set; }

public virtual int Id { get; set; }

您正在向 DbExtensions.Include Method 传递无效参数,因为它需要

A lambda expression representing the path to include.

而不是您指定的条件:

c => c.ArticleId == articleViewModel.Id

您需要以不同的方式调用 FindAll

AttachmentFileBLL
    .Instance
    .FindAll(c => c.ArticleId)
    .ToList();

这将指定 属性。

但是由于您还需要为此指定导航 属性,因此您需要在那里使用这样的 属性。我不知道它在你的模型中可以有什么名字,但也许是这样的:

AttachmentFileBLL
    .Instance
    .FindAll(c => c.Articles) // Assuming 'Articles' is a navigation property.
    .ToList();

如果您只想获得一些物品,您应该将条件放在其他 Where 中,以满足您的需要:

AttachmentFileBLL
   .Instance
   .SomeCollection
   .Where(c => c.ArticleId == articleViewModel.Id)
   .FindAll(c => c.Articles)
   .ToList();