Skip to content

Instantly share code, notes, and snippets.

@PadreSVK
Created Apr 12, 2021
Embed
What would you like to do?
DBContext registration with design time factory and usage of extension method GetConnectionString
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=.\\SQLEXPRESS;Initial Catalog=OpenWeather;Persist Security Info=True;Integrated Security=true;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"OpenWeatherHttpClientOptions": {
"Url": "https://api.openweathermap.org/data/2.5/",
"Key" : "take a look to user secrets :X"
}
}
using System;
using System.Text;
using BackgroundServices;
using BackgroundServices.HttpClients;
using BackgroundServices.Options;
using BL;
using DAL;
using DAL.Models.Identity;
using DAL.Query.Abstraction;
using DAL.Query.AllCity;
using DAL.Repositories;
using DAL.Repositories.Abstraction;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace OpenWeather
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// it register DBContext with scoped lifetime
services.AddDbContext<OpenWeatherDbContext>(
options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
//register all repositories with scoped lifetime => https://github.com/khellang/Scrutor
services.Scan(a =>
//all concrete repositories must be in same assembly
a.FromAssemblyOf<CityRepository>()
.AddClasses(c => c.AssignableTo(typeof(IRepository<,>)))
.AsImplementedInterfaces()
.WithScopedLifetime()
);
//...
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
"default",
"{controller=Home}/{action=Index}/{id?}");
});
}
}
public class Factory : IDesignTimeDbContextFactory<OpenWeatherDbContext>
{
public OpenWeatherDbContext CreateDbContext(string[] args)
{
var contextOptionsBuilder = new DbContextOptionsBuilder<OpenWeatherDbContext>();
contextOptionsBuilder.UseSqlServer(
"Data Source=.\\SQLEXPRESS;Initial Catalog=OpenWeather;Persist Security Info=True;Integrated Security=true;");
return new OpenWeatherDbContext(contextOptionsBuilder.Options);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment