【发布时间】:2012-12-28 05:42:41
【问题描述】:
我有一个使用 .NET 框架 4.0 和 LINQ to SQL 的 ASP.NET MVC 3 项目。因为我在第一次执行某些查询时遇到了严重的性能问题,所以我决定使用 Entity Framework 5 来利用新的 LINQ 自动编译功能。
现在我已经安装了 VS 2012、.NET Framework 4.5 和 Entity Framework 5,并将我的项目更改为指向 .NET Framework 4.5,并更新了我的数据上下文文件以使用 EF5。查询现在具有完全相同的性能,其中一些在第一次执行时非常慢,甚至我得到一个超时异常,第二次一切都很好,我想知道问题是否是迁移过程可能不是正确,我仍在使用 EF4,或者这只是我的查询构造的问题,由于未知原因无法使用自动编译功能。
EntityFramework dll 有 5.0 版本,System.Data.Entity dll 有 4 版本,没错,不是吗?有什么建议吗?
我包含了最有问题的查询,它检索了我需要通过 2 个查询构建的网格(Telerik MVC 网格)的分页结果(最初是带有子查询的 SQL 语句):
/// <summary>
/// Get the elements for the AppLog grid.
/// </summary>
public void GetAppLogElements(
int clientID, string language, int functionID, int errorLevel,
int numPage, int pageSize, IList<IFilterDescriptor> filterDescriptors, IList<SortDescriptor> sortDescriptors, IList<GroupDescriptor> groupDescriptors,
ref IQueryable<Model_AppLog> rows, ref int numRows)
{
string orderString = string.Empty;
var items =
from ap in objDataContext.applicationLogs
where
ap.clientID == clientID &&
ap.recordGroup != null
join alf in objDataContext.appLogFunctions on
new { functionID = ap.functionID.Value } equals new { functionID = alf.functionID }
join ale in objDataContext.appLogErrorLevels on
new { errorLevel = ap.errorLevel.Value } equals new { errorLevel = ale.errorLevelID }
join als in objDataContext.appLogSeverities on
new { severity = ap.severity.Value } equals new { severity = als.severityID }
group new { ap, alf, als } by new { ap.functionID, ap.recordGroup, ap.clerkID } into queryGrouped
select new Model_AppLog()
{
sequence = queryGrouped.Max(c => c.ap.sequence),
functionID = queryGrouped.Key.functionID,
recordGroup = queryGrouped.Key.recordGroup,
clerkID = queryGrouped.Key.clerkID,
date = queryGrouped.Min(c => c.ap.date),
errorLevel = (queryGrouped.Max(c => c.ap.errorLevel) == null || !queryGrouped.Max(c => c.ap.errorLevel).HasValue ? 0 : queryGrouped.Max(c => c.ap.errorLevel)),
severity = queryGrouped.Max(c => c.ap.severity)
};
if (errorLevel != -1)
items = items.Where(column => column.errorLevel >= errorLevel);
var _items =
from subSelect in items
join alf in objDataContext.appLogFunctions on
new { functionID = subSelect.functionID.Value } equals new { functionID = alf.functionID }
join alft in objDataContext.appLogFunctionTexts on
new { alf.functionID, language } equals new { alft.functionID, alft.language }
join ale in objDataContext.appLogErrorLevels on
new { errorLevel = subSelect.errorLevel.Value } equals new { errorLevel = ale.errorLevelID }
join alet in objDataContext.appLogErrorLevelTexts on
new { errorLevelID = subSelect.errorLevel.Value, language } equals new { alet.errorLevelID, alet.language }
join als in objDataContext.appLogSeverities on
new { severity = subSelect.severity.Value } equals new { severity = als.severityID }
join alst in objDataContext.appLogSeverityTexts on
new { als.severityID, language } equals new { alst.severityID, alst.language }
select new Model_AppLog()
{
sequence = subSelect.sequence,
functionID = subSelect.functionID,
recordGroup = subSelect.recordGroup,
clerkID = subSelect.clerkID,
date = subSelect.date,
errorLevel = subSelect.errorLevel,
severity = subSelect.severity,
functionDescription = alft.denotation,
errorLevelDescription = alet.denotation,
severityDescription = alst.denotation
};
//Apply filters
if (filterDescriptors != null && filterDescriptors.Any())
{
_items = _items.Where(ExpressionBuilder.Expression<Model_AppLog>(filterDescriptors));
}
if (functionID != -1)
_items = _items.Where(column => column.functionID == functionID);
//Apply sorting
if (sortDescriptors != null)
{
GlobalMethods objGlobalMethods = new GlobalMethods();
orderString = objGlobalMethods.GetOrderString(sortDescriptors);
}
//Apply ordering
if (orderString != string.Empty)
_items = _items.OrderBy(orderString);
else
_items = _items.OrderByDescending(x => x.date);
//Set total number of rows
numRows = _items.AsEnumerable<Model_AppLog>().Count();
//Get paginated results
_items = _items.Skip(pageSize * (numPage - 1)).Take(pageSize);
//Set data result
rows = _items;
}
【问题讨论】:
-
首先,要调试性能问题,您应该使用 SQL Profiler。其次,要迁移到 EF5,需要添加 EF5.x DbContext Generator,并更改一些 api,例如“context.AddToPost --> context.Post.Add()”,没什么特别的。
-
是的,也许我还应该检查 SQL 分析器。我需要什么“EF5.x DbContext Generator”这种文件到底是什么?没有它,我的项目目前可以正常工作,我刚刚创建了一个 ADO.NET 实体数据模型,仅此而已。
标签: asp.net-mvc entity-framework-4 linq-to-entities entity-framework-5 auto-compile