gpt4 book ai didi

c# - EntityFramework.dll 中出现 'System.ArgumentException' 类型的异常,但未在用户代码中处理

转载 作者:太空宇宙 更新时间:2023-11-03 13:11:57 25 4
gpt4 key购买 nike

我遇到的问题是:

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

查找所有方法:

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

Error location

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

关于c# - EntityFramework.dll 中出现 'System.ArgumentException' 类型的异常,但未在用户代码中处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28148808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com