我有我的dotnet核心項目與實體框架核心和mysql連接器的性能問題。在我的控制器http://insidemysql.com/howto-starting-with-mysql-ef-core-provider-and-connectornet-7-0-4/增加實體框架連接和查詢
public class MhdContext : DbContext
{
public MhdContext(DbContextOptions<MhdContext> options) : base(options) { }
public DbSet<Vehicle> Vehicles { get; set; }
}
public static class MhdContextFactory
{
public static MhdContext Create(string connectionString)
{
var optionsBuilder = new DbContextOptionsBuilder<MhdContext>();
optionsBuilder.UseMySQL(connectionString);
//Ensure database creation
var context = new MhdContext(optionsBuilder.Options);
context.Database.EnsureCreated();
var serviceProvider = context.GetInfrastructure<IServiceProvider>();
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
loggerFactory.AddProvider(new MyLoggerProvider());
return context;
}
}
我調用這個方法:
我創建的DbContext通過下面的教程
public async Task<Vehicle[]> GetAllVehicles()
{
_logger.LogInformation("Getting All vehicles");
using (var dbContext = MhdContextFactory.Create(_dbConfig.ConnectionString))
{
var vehicles = await dbContext.Vehicles.AsNoTracking()
.FromSql("SELECT * FROM Vehicles AS v group by v.VehicleId order by LastUpdate")
.ToArrayAsync();
return vehicles;
}
}
第一要求一切看起來像工作
有幾個請求後後是重複的sql命令並且正在增加 第7次請求日誌:https://www.dropbox.com/s/2ezyowyubq1orsn/7thRequest.txt?dl=0
你能幫我解決我做錯了什麼嗎?