gpt4 book ai didi

c# - 如何使用 EF6 获取大量记录

转载 作者:行者123 更新时间:2023-12-04 07:44:19 24 4
gpt4 key购买 nike

我需要使用 EF6 从 SQL Server 数据库中获取大量记录。它需要很多时间的问题。主要问题是名为 Series 的实体。其中包含 Measurements .其中大约有 250K,每个都有 2 个嵌套实体,称为 FrontDropPhotoSideDropPhoto .

[Table("Series")]
public class DbSeries
{
[Key] public Guid SeriesId { get; set; }
public List<DbMeasurement> MeasurementsSeries { get; set; }
}

[Table("Measurements")]
public class DbMeasurement
{
[Key] public Guid MeasurementId { get; set; }

public Guid CurrentSeriesId { get; set; }
public DbSeries CurrentSeries { get; set; }

public Guid? SideDropPhotoId { get; set; }
[ForeignKey("SideDropPhotoId")]
public virtual DbDropPhoto SideDropPhoto { get; set; }

public Guid? FrontDropPhotoId { get; set; }
[ForeignKey("FrontDropPhotoId")]
public virtual DbDropPhoto FrontDropPhoto { get; set; }
}

[Table("DropPhotos")]
public class DbDropPhoto
{
[Key] public Guid PhotoId { get; set; }
}
我写了这样的 fetch 方法(为了清楚起见,省略了大多数属性):
public async Task<List<DbSeries>> GetSeriesByUserId(Guid dbUserId)
{
using (var context = new DDropContext())
{
try
{
var loadedSeries = await context.Series
.Where(x => x.CurrentUserId == dbUserId)
.Select(x => new
{
x.SeriesId,
}).ToListAsync();

var dbSeries = new List<DbSeries>();

foreach (var series in loadedSeries)
{
var seriesToAdd = new DbSeries
{
SeriesId = series.SeriesId,
};

seriesToAdd.MeasurementsSeries = await GetMeasurements(seriesToAdd);

dbSeries.Add(seriesToAdd);
}

return dbSeries;
}
catch (SqlException e)
{
throw new TimeoutException(e.Message, e);
}
}
}

public async Task<List<DbMeasurement>> GetMeasurements(DbSeries series)
{
using (var context = new DDropContext())
{
var measurementForSeries = await context.Measurements.Where(x => x.CurrentSeriesId == series.SeriesId)
.Select(x => new
{
x.CurrentSeries,
x.CurrentSeriesId,
x.MeasurementId,
})
.ToListAsync();

var dbMeasurementsForAdd = new List<DbMeasurement>();

foreach (var measurement in measurementForSeries)
{
var measurementToAdd = new DbMeasurement
{
CurrentSeries = series,
MeasurementId = measurement.MeasurementId,
FrontDropPhotoId = measurement.FrontDropPhotoId,
FrontDropPhoto = measurement.FrontDropPhotoId.HasValue
? await GetDbDropPhotoById(measurement.FrontDropPhotoId.Value)
: null,
SideDropPhotoId = measurement.SideDropPhotoId,
SideDropPhoto = measurement.SideDropPhotoId.HasValue
? await GetDbDropPhotoById(measurement.SideDropPhotoId.Value)
: null,
};

dbMeasurementsForAdd.Add(measurementToAdd);
}

return dbMeasurementsForAdd;
}
}

private async Task<DbDropPhoto> GetDbDropPhotoById(Guid photoId)
{
using (var context = new DDropContext())
{
var dropPhoto = await context.DropPhotos
.Where(x => x.PhotoId == photoId)
.Select(x => new
{
x.PhotoId,
}).FirstOrDefaultAsync();

if (dropPhoto == null)
{
return null;
}

var dbDropPhoto = new DbDropPhoto
{
PhotoId = dropPhoto.PhotoId,
};

return dbDropPhoto;
}
}
通过 FluentAPI 配置的关系:
        modelBuilder.Entity<DbSeries>()
.HasMany(s => s.MeasurementsSeries)
.WithRequired(g => g.CurrentSeries)
.HasForeignKey(s => s.CurrentSeriesId)
.WillCascadeOnDelete();

modelBuilder.Entity<DbMeasurement>()
.HasOptional(c => c.FrontDropPhoto)
.WithMany()
.HasForeignKey(s => s.FrontDropPhotoId);

modelBuilder.Entity<DbMeasurement>()
.HasOptional(c => c.SideDropPhoto)
.WithMany()
.HasForeignKey(s => s.SideDropPhotoId);
我需要所有这些数据来填充 WPF DataGrid。显而易见的解决方案是向这个 DataGrid 添加分页。这个解决方案很诱人,但它会严重破坏我的应用程序的逻辑。我想在运行时使用这些数据创建绘图,所以我需要所有这些,而不仅仅是某些部分。我试图通过使每种方法都使用 async await 来优化它,但这还不够有用。我试过添加
.Configuration.AutoDetectChangesEnabled = false;
对于每个上下文,但加载时间仍然很长。如何解决这个问题?

最佳答案

首先,async/await不会在这里帮助你。这不是“更快”类型的操作,而是关于适应“在此操作进行计算时可以做其他事情”的系统。如果有的话,它会进行操作 较慢 以换取使系统更具响应性。
我的建议是将您的关注点分开:一方面您希望显示详细数据。另一方面,您想绘制一个整体图。把这些分开。用户不需要一次查看每条记录的详细信息,在服务器端对其进行分页将大大减少任何时候的原始数据量。图形想要查看所有数据,但他们并不关心像位图这样的“重”细节。
接下来的事情是将您的 View 模型与域模型(实体)分开。做这样的事情:

var measurementToAdd = new DbMeasurement
{
CurrentSeries = series,
MeasurementId = measurement.MeasurementId,
FrontDropPhotoId = measurement.FrontDropPhotoId,
FrontDropPhoto = measurement.FrontDropPhotoId.HasValue
? await GetDbDropPhotoById(measurement.FrontDropPhotoId.Value)
: null,
SideDropPhotoId = measurement.SideDropPhotoId,
SideDropPhoto = measurement.SideDropPhotoId.HasValue
? await GetDbDropPhotoById(measurement.SideDropPhotoId.Value)
: null,
};
……自找麻烦。任何接受 DbMeasurement 的代码都应接收完整的或可完成的 DbMeasurement,而不是部分填充的实体。它 将来烧死你。为数据网格定义一个 View 模型并填充它。通过这种方式,您可以清楚地区分什么是实体模型,什么是 View 模型。
接下来,对于数据网格,绝对实现服务端分页:
public ICollection<MeasurementViewModel> GetMeasurements(int seriesId, int pageNumber, int pageSize)
{
using (var context = new DDropContext())
{
var measurementsForSeries = await context.Measurements
.Where(x => x.CurrentSeriesId == seriesId)
.Select(x => new MeasurementViewModel
{
MeasurementId = x.MeasurementId,
FromDropPhoto = x.FromDropPhoto.ImageData,
SideDropPhoto = x.SideDropPhoto.ImageData
})
.Skip(pageNumber*pageSize)
.Take(pageSize)
.ToList();

return measurementsForSeries;
}
}
这假设我们想要为行提取图像数据(如果可用)。在查询中利用相关数据的导航属性,而不是迭代结果并为每一行返回数据库。
对于图表,您可以返回原始整数数据或仅需要字段的数据结构,而不是依赖为网格返回的数据。它可以为整个表拉动,而无需“重”图像数据。当数据可能已经加载一次时,转到数据库似乎适得其反,但结果是两个高效的查询,而不是一个试图服务于两个目的的非常低效的查询。

关于c# - 如何使用 EF6 获取大量记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67274280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com