EF3 核心内存数据库 OrderBy "sequence contains no elements" 异常
EF3 core in-memory database OrderBy "sequence contains no elements" exception
这在连接到 SQL 时工作正常,但在使用内存数据库时抛出异常
Actors
集合在 Movie
构造函数中初始化为 new List<Actor>
。
public class Actor
{
public int ActorId { get; set; }
public string Name { get; set; }
}
public class Movie
{
public Movie()
{
Actors = new List<Actor>();
}
public int MovieId { get; set; }
public string Name { get; set; }
public virtual ICollection<Actor> Actors { get; set; }
}
var searchQuery = context.Movies.Where(m => m.Name == "Jaws");
searchQuery = searchQuery.OrderBy(m => m.Actors.Min(a => a.BirthDate ?? DateTime.MaxValue));
试试这个,在构造函数中删除 Actor = new List<Actor>
public Movie()
{
}
在您的查询中 .Include(m => m.Actor)
var searchQuery = context.Movies.Include(m => m.Actors).Where(m => m.Name == "Jaws");
这在连接到 SQL 时工作正常,但在使用内存数据库时抛出异常
Actors
集合在 Movie
构造函数中初始化为 new List<Actor>
。
public class Actor
{
public int ActorId { get; set; }
public string Name { get; set; }
}
public class Movie
{
public Movie()
{
Actors = new List<Actor>();
}
public int MovieId { get; set; }
public string Name { get; set; }
public virtual ICollection<Actor> Actors { get; set; }
}
var searchQuery = context.Movies.Where(m => m.Name == "Jaws");
searchQuery = searchQuery.OrderBy(m => m.Actors.Min(a => a.BirthDate ?? DateTime.MaxValue));
试试这个,在构造函数中删除 Actor = new List<Actor>
public Movie()
{
}
在您的查询中 .Include(m => m.Actor)
var searchQuery = context.Movies.Include(m => m.Actors).Where(m => m.Name == "Jaws");