using Microsoft.EntityFrameworkCore; using Monitor.Models; namespace DataBase.MySql.Tool { public sealed class DBTool { private static readonly Lazy MLazy = new Lazy(() => new DBTool()); public static DBTool Instance => MLazy.Value; private AsyncLocal _dbContext = new AsyncLocal(); private DBTool() { } public MyDbContext MyDbContext() { MyDbContext? myDbContext = _dbContext.Value; if (myDbContext == null) { DbContextOptionsBuilder optionsBuilder = new DbContextOptionsBuilder(); optionsBuilder.UseMySQL( "Data Source=localhost;Initial Catalog=chassis_track_swap;User ID=root;Password=123456;sslmode=none;"); myDbContext = new MyDbContext(optionsBuilder.Options); } _dbContext.Value = myDbContext; return myDbContext; } } }