Entity Framework very slow compared to SQL Query

MOHAMMED SHIYAS P A 1 Reputation point
2022-06-19T14:11:26.137+00:00

Hi,

I have a Stored Procedure which returns the result within a second.
When I call the stored procedure from my Web API it takes at least 15 seconds to return the data. Sometimes goes upto few minutes.
I am using the below method. It returns around 15000 records with size around 2 MB.

public async Task<IEnumerable<Find>> GetFinds(int level)
{
var finds = _context.Finds.FromSqlRaw<Find>("sp_SHT_SelectAllValuesGivenLevels_Find {0}", level).ToListAsync();
return await finds;
}

Model:

[Keyless]
public class Find
{
[Column(TypeName="varchar")]
public string ID { get; set; }
[Column(TypeName="varchar")]
public string Description { get; set; }
}

DBContext:

public class PHTDbContext : DbContext
{
public PHTDbContext(DbContextOptions<PHTDbContext> options) : base(options)
{
}
public DbSet<Find> Finds { get; set; }
}

I am redesigning the application from ASP.Net WebForms to ASP.Net Core Web API + Angular.
The redesign should make the application faster but it is slowing down.
Please help me with this.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
698 questions
{count} votes

1 answer

Sort by: Most helpful
  1. JD N 1 Reputation point
    2022-12-07T20:30:24.697+00:00

    Where you have, ToListAsync(); , replace it with .AsNoTracking().ToListAsync();
    That should help quite a bit.

    0 comments No comments