我使用Entity Framework Core 1.1.0和EntityFrameworkCore.SqlServer 1.1.0作为DataBase提供程序(从注释添加此信息)。和SQL Server 2014.
我有这个型号:
public class User
{
public int Id { get; set; }
public virtual ICollection<IdentityUserRole> Roles { get; }
}
public class IdentityUserRole
{
public int RoleId { get; set; }
public int UserId { get; set; }
}
我使用此代码:
IQueryable<User> query = context.Set<User>();
IQueryable<User> query2 = query.Include(u => u.Roles);
当我调用query.ToList()
时,我有一个SQL查询:
SELECT [u].[Id], [u].[Email], [u].[UserName]
FROM [User] AS [u]
当我致电query2.ToList()
时,我有两个SQL查询:
的 1
SELECT [u].[Id], [u].[Email], [u].[UserName]
FROM [User] AS [u]
ORDER BY [u].[Id]
2
SELECT [i].[UserId], [i].[RoleId]
FROM [IdentityUserRole<int>] AS [i]
WHERE EXISTS (
SELECT 1
FROM [User] AS [u]
WHERE [i].[UserId] = [u].[Id])
ORDER BY [i].[UserId]
为什么EF在SQL查询中添加ORDER BY [u].[Id]
和ORDER BY [i].[UserId]
?一切正常,没有任何ORDER BY
陈述
我有这个问题,因为如果我调用query2.OrderByDescending(q => q.Id)
我有这个无效的SQL:
SELECT [u].[Id], [u].[Email], [u].[UserName]
FROM [User] AS [u]
ORDER BY [u].[Id] DESC, [u].[Id]
SQL-server返回异常:
已按列表顺序多次指定列。