我是EF 6.0的初学者,我开始设计一个易于使用的DAL。 以下是我对每个实体的所有操作任务的界面:
public interface IEntity<T>
{
T Insert(T NewItem);
T Update(T ModifiedItem);
bool Delete(T Selected);
bool DeleteByID(int ItemID);
List<T> Select(Func<T, bool> Query);
IQueryable<T> GetQuery();
}
这是我在&#39; Group&#39;上的Select方法的实现。实体:
public bool Select(Func<DataAccess.Model.Group, bool> Query, out List<Business.Entity.Login.LoginUser> Output)
{
IQueryable<DataAccess.Model.Group> Connection = GetQuery();
Output = Connection.Where(Query).ToLoginUser().ToList();
return true;
}
我将使用Func&lt;,&gt;接收字段上的所有范围限制器参数和我使用下面的扩展方法来隐藏我不需要的一些列:
public static IEnumerable<BEntities.Login.LoginUser> ToLoginUser(this IEnumerable<DataAccess.Model.Group> Model)
{
return from A in Model select new Business.Entity.Login.LoginUser { Username = A.Name, Password = A.Identifier };
}
但实际上在SQL Server上运行的内容(在诊断窗口中)是:
SELECT
[Extent1].[ID] AS[ID],
[Extent1].[Identifier] AS [Identifier],
[Extent1].[Name] AS [Name],
[Extent1].[ParentGroup] AS [ParentGroup],
[Extent1].[GroupPath] AS [GroupPath]
FROM[dbo].[Group] AS [Extent1]
我看不到任何&#39; Where&#39;此查询的子句或任何列限制。 我的错在哪里?
答案 0 :(得分:1)
使用Expression&lt; ...,...&gt;将生成的SQL查询更改为类似的内容:
[Extent1].[ID] AS [ID],
[Extent1].[Identifier] AS [Identifier],
[Extent1].[Name] AS [Name],
[Extent1].[ParentGroup] AS [ParentGroup],
[Extent1].[GroupPath] AS [GroupPath]
FROM [dbo].[Group] AS [Extent1]
WHERE N'Noei' = [Extent1].[Name]
但我也使用了模型映射器来限制它的一些列:
public static IEnumerable<BEntities.Login.LoginUser> ToLoginUser(this IEnumerable<DataAccess.Model.Group> Model)
{
return from A in Model select new Business.Entity.Login.LoginUser { Username = A.Name, Password = A.Identifier };
}
我用这种方式调用它:
public bool Select(Expression<Func<DataAccess.Model.Group, bool>> Query, out List<Business.Entity.Login.LoginUser> Output)
{
IQueryable<DataAccess.Model.Group> Connection = GetQuery();
Output = Connection.Where(Query).ToLoginUser().ToList();
return true;
}
但我希望在生成的Query中看到列隐藏。