我正在尝试使用 EF 将新记录添加到 SQL 数据库中。代码看起来像
public void Add(QueueItem queueItem)
{
var entity = queueItem.ApiEntity;
var statistic = new Statistic
{
Ip = entity.Ip,
Process = entity.ProcessId,
ApiId = entity.ApiId,
Result = entity.Result,
Error = entity.Error,
Source = entity.Source,
DateStamp = DateTime.UtcNow,
UserId = int.Parse(entity.ApiKey),
};
_statisticRepository.Add(statistic);
unitOfWork.Commit();
}
统计实体中有导航 Api 和 User 属性,我想将其加载到新的统计实体中。我尝试使用下面的代码加载导航属性,但它会产生大量查询并降低性能。关于如何以其他方式加载导航属性的任何建议?
public Statistic Add(Statistic statistic)
{
_context.Statistic.Include(p => p.Api).Load();
_context.Statistic.Include(w => w.User).Load();
_context.Statistic.Add(statistic);
return statistic;
}
有些人可能会问为什么我要在添加新实体时加载导航属性,这是因为我在将实体移动到数据库之前在 DbContext.SaveChanges() 中执行了一些计算。代码看起来像
public override int SaveChanges()
{
var addedStatistics = ChangeTracker.Entries<Statistic>().Where(e => e.State == EntityState.Added).ToList().Select(p => p.Entity).ToList();
var userCreditsGroup = addedStatistics
.Where(w => w.User != null)
.GroupBy(g => g.User )
.Select(s => new
{
User = s.Key,
Count = s.Sum(p=>p.Api.CreditCost)
})
.ToList();
//Skip code
}
所以上面的 Linq 在不加载导航属性的情况下将无法工作,因为它使用它们。
我还添加了完整 View 的统计实体
public class Statistic : Entity
{
public Statistic()
{
DateStamp = DateTime.UtcNow;
}
public int Id { get; set; }
public string Process { get; set; }
public bool Result { get; set; }
[Required]
public DateTime DateStamp { get; set; }
[MaxLength(39)]
public string Ip { get; set; }
[MaxLength(2083)]
public string Source { get; set; }
[MaxLength(250)]
public string Error { get; set; }
public int UserId { get; set; }
[ForeignKey("UserId")]
public virtual User User { get; set; }
public int ApiId { get; set; }
[ForeignKey("ApiId")]
public virtual Api Api { get; set; }
}
请您参考如下方法:
如您所说,以下针对您的上下文的操作将生成大型查询:
_context.Statistic.Include(p => p.Api).Load();
_context.Statistic.Include(w => w.User).Load();
这些是将所有统计信息和关联的 api 实体的对象图具体化,然后将所有统计信息和关联的用户具体化到统计上下文中
只需将其替换为如下所示的单个调用即可将其减少为单次往返:
_context.Statistic.Include(p => p.Api).Include(w => w.User).Load();
加载这些后, Entity Framework 更改跟踪器将修复新统计实体上的关系,从而一次性为所有新统计填充 api 和用户的导航属性。
根据一次性创建的新统计数据与数据库中现有统计数据的数量,我非常喜欢这种方法。
但是,查看 SaveChanges 方法,它看起来像是每个新统计数据都发生一次关系修复。 IE。每次添加新统计信息时,您都会在数据库中查询所有统计信息以及关联的 API 和用户实体,以触发新统计信息的关系修复。
在这种情况下我更倾向于做以下事情:
_context.Statistics.Add(statistic);
_context.Entry(statistic).Reference(s => s.Api).Load();
_context.Entry(statistic).Reference(s => s.User).Load();
这将只查询新统计的 Api 和 User,而不是所有统计。也就是说,您将为每个新统计数据生成 2 个单行数据库查询。
或者,如果您在一批中添加大量统计信息,您可以通过预先加载所有用户和 api 实体来使用上下文中的本地缓存。 IE。提前命中以将所有用户和 api 实体预缓存为 2 个大型查询。
// preload all api and user entities
_context.Apis.Load();
_context.Users.Load();
// batch add new statistics
foreach(new statistic in statisticsToAdd)
{
statistic.User = _context.Users.Local.Single(x => x.Id == statistic.UserId);
statistic.Api = _context.Api.Local.Single(x => x.Id == statistic.ApiId);
_context.Statistics.Add(statistic);
}
有兴趣了解 Entity Framework 是否从其本地缓存中进行关系修复。 IE。如果以下内容将在所有新统计信息上填充本地缓存中的导航属性。以后有戏了。
_context.ChangeTracker.DetectChanges();
免责声明:所有代码都直接输入到浏览器中,因此请注意拼写错误。
