[英]EF - not supported in LINQ to Entities
I am trying to list some food items with a controller. 我试图用控制器列出一些食品。 I use Repository pattern with UnitOfWork for the data in another assembly and referenced it in a BaseApiController.
我将Repository模式与UnitOfWork一起用于另一个程序集中的数据,并在BaseApiController中引用它。 The Data property is my UnitOfWork instance.
Data属性是我的UnitOfWork实例。
var result = Data.Food
.FindAll()
.Select(FoodItemViewModel.Create);
return result;
and here is my ViewModel: 这是我的ViewModel:
public static Expression<Func<FoodItem, FoodItemViewModel>> Create
{
get
{
return fi => new FoodItemViewModel
{
Id = fi.Id,
Description = fi.Description,
DiaryEntries = fi.DiaryEntries
.Select(s => new DiaryEntityViewModel()
{
Id = s.Id,
Quantity = s.Quantity
}
};
}
}
But all I get is: 但我得到的只是:
"The specified type member 'DiaryEntries' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported."
“LINQ to Entities不支持指定的类型成员'DiaryEntries'。仅支持初始值设定项,实体成员和实体导航属性。”
My DiaryEntries member in the ViewModel is 我在ViewModel中的DiaryEntries成员是
IEnumerable<DiaryEntityViewModel>
and my DiaryEntries member in the Data instance is 和我的数据实例中的DiaryEntries成员是
IRepository<DiaryEntry>
and DiaryEntry is my model class 和DiaryEntry是我的模特课
and here is my FoodItem model class: 这是我的FoodItem模型类:
public class FoodItem
{
private IEnumerable<Measure> measures;
private IEnumerable<DiaryEntry> diaryEntries;
public FoodItem()
{
this.measures = new HashSet<Measure>();
this.diaryEntries = new HashSet<DiaryEntry>();
}
public int Id { get; set; }
public string Description { get; set; }
public virtual IEnumerable<DiaryEntry> DiaryEntries
{
get
{
return this.diaryEntries;
}
set
{
this.diaryEntries = value;
}
}
public virtual IEnumerable<Measure> Measures
{
get
{
return this.measures;
}
set
{
this.measures = value;
}
}
}
Change you FoodItem
class to the one below, IEnumerable<T>
is not supported as a type for a navigation collection : 将
FoodItem
类更改为下面的类, IEnumerable<T>
不支持作为导航集合的类型 :
public class FoodItem
{
public FoodItem()
{
this.Measures = new HashSet<Measure>();
this.DiaryEntries = new HashSet<DiaryEntry>();
}
public int Id { get; set; }
public string Description { get; set; }
public virtual ICollection<DiaryEntry> DiaryEntries
{
get;
set;
}
public virtual ICollection<Measure> Measures
{
get;
set;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.