I have a problem with Entity Framework Core.
I made an admin panel for monitoring in "Blazor Server" with stardate and to date users choose and every click search with from date && to date memory increment 200mb, 300mb, 400mb, 1gb..
I need don't increment memory after call dbcontext..
Method call:
var data = await Task.Run(() => Service.GetJasminData(fromdate, todate)?.OrderBy(x => x.SendDate).ToList());
Method:
protected readonly ApplicationDBContext _dBContext;
public SmsService(ApplicationDBContext _db)
{
_dBContext = _db;
}
public List<Sms> GetJasminData(DateTime fromdate, DateTime todate, int count = 30)
{
return _dBContext.Jasmin.Where(x => x.SendDate >= fromdate && x.SendDate <= todate).ToList();
}
CodePudding user response:
Whenever you retrieve data from the database, by default EF starts to track this information so it can know what has been changed to update the data during a SaveChanges()
call.
You may have some memory leak issue, it is worth taking a look if your dbcontext is behaving correctly, probably not since the memory usage seems to be increasing. Blazor Server can be tricky since it deals in a very different way with calls. How are you registering the Context injection?
One approach you could take is to use AsNoTracking()
, this will prevent EF from tracking your entities which will decrease your memory usage, for a read-only call that's the best approach.
I also recommend you avoid Task.Run()
, EF has some built-in methods that are ready to deal with async calls and have internally optimized.
Repository methods
protected readonly ApplicationDBContext _dBContext;
public SmsService(ApplicationDBContext _db)
{
_dBContext = _db;
}
public async Task<List<Sms>> GetJasminData(DateTime fromdate, DateTime todate, int count = 30)
{
return await _dBContext.Jasmin.AsNoTracking().Where(x => x.SendDate >= fromdate && x.SendDate <= todate).ToListAsync();
}
method call
var data = await Service.GetJasminData(fromdate, todate)?.OrderBy(x => x.SendDate).ToList();
Thats something that may worth the reading: https://learn.microsoft.com/en-us/aspnet/core/blazor/blazor-server-ef-core?view=aspnetcore-7.0
CodePudding user response:
Page Code
protected async Task SearchFilter()
{
if (fromdate != DateTime.MinValue && todate != DateTime.MinValue && string.IsNullOrEmpty(searchText))
{
//SplitData(Service.GetAll().Where(x => x.SendDate >= fromdate && x.SendDate <= todate)?.OrderBy(x => x.SendDate)?.Take(40).ToList());
var hasData = await Task.Run(() => Service.GetJasminData(fromdate, todate)?.OrderBy(x => x.SendDate).ToList());
if (hasData != null)
{
SplitData(hasData);
}
fromdate = DateTime.MinValue;
}
else if (fromdate != DateTime.MinValue && todate != DateTime.MinValue && !string.IsNullOrEmpty(searchText))
{
var hasData = await Task.Run(() => Service.GetJasminData(fromdate, todate).Where(x => x.ToString().Contains(searchText)).ToList());
if (hasData != null)
{
SplitData(hasData);
}
}
else if ((fromdate == DateTime.MinValue || todate == DateTime.MinValue) && !string.IsNullOrEmpty(searchText))
{
var hasData = await Task.Run(() => Service.GetAllByText(searchText).ToList());
if (hasData != null)
{
SplitData(hasData);
}
}
}