简体   繁体   English

使用Entity Framework加载具有大字符串属性的实体时性能不佳

[英]Poor performance when loading entity with large string property using Entity Framework

Given an entity and a DbContext like the following: 给定一个实体和一个DbContext,如下所示:

public class Entity
{
    public int Id {get;set;}
    public string LargeString {get;set;}
}

public class MyDbContext : DbContext
{
    public DbSet<Entity> Entities {get;set;}
}

And an entity stored in the database with Id 42 and LargeString containing some two megabytes of XML. 还有一个存储在数据库中的实体,其Id 42和LargeString包含大约2 MB的XML。 The following takes half a minute or so, and occasionally gives OutOfMemoryException : 以下过程需要半分钟左右的时间,并且偶尔会出现OutOfMemoryException

using (var dbContext = new MyDbContext())
{
    var entity = await dbContext.Entities.SingleAsync(e => e.Id == 42);
}

At the same time, the following Dapper query executes in miliseconds: 同时,以下Dapper查询将在几毫秒内执行:

using (var dbContext = new MyDbContext())
{
    var entity = await dbContext.Database.Connection
        .Query<Entity>("SELECT Id, LargeString FROM Entities WHERE Id = 42")
        .SingleAsync();
}

Is it possible to somehow hint Entity Framework that the LargeString property could be this large (in a way that would make Entity Framework perform acceptable in this scenario). 是否可能以某种方式暗示实体框架LargeString属性可能这么大(以这种方式可以使实体框架在这种情况下可以接受)。

I had the same issue yesterday. 我昨天有同样的问题。 What I did find out is that async operations with Entity Framework is broken or at least very slow. 我确实发现,使用Entity Framework的异步操作已损坏或至少非常缓慢。 Try using the same operations synchronously: 尝试同步使用相同的操作:

using (var dbContext = new MyDbContext())
{
    var entity = dbContext.Entities.Single(e => e.Id == 42);
}

Also read the post by rducom here: https://stackoverflow.com/a/28619983/7108481 另请在此处阅读rducom的帖子: https : //stackoverflow.com/a/28619983/7108481

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM