public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } DbSet<SimulationParcelData> SimulationParcelData { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<SimulationParcelData>().ToTable("SimulationParcels"); }
.ConfigureServices((hostContext, services) => { //var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>(); //optionsBuilder.UseSqlServer("Server=.\\SQLEXPRESS;Database=xxx;Trusted_Connection=True;");//, //services.AddScoped<ApplicationDbContext>(s => new ApplicationDbContext(optionsBuilder.Options)); services.AddDbContext<ApplicationDbContext>(options => { options.UseSqlServer( hostContext.Configuration["ConnectionStrings:Connection"], serverDbContextOptionsBuilder => { var minutes = (int)TimeSpan.FromMinutes(3).TotalSeconds; serverDbContextOptionsBuilder.CommandTimeout(minutes); serverDbContextOptionsBuilder.EnableRetryOnFailure(); }); }); services.AddScoped<ApplicationDbContext>(); services.AddHostedService<Worker>(); });
Consuming a scoped service in a background task - Background tasks with hosted services in ASP.NET Core | Microsoft Docs[^] To use scoped services within a BackgroundService, create a scope. No scope is created for a hosted service by default.
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)