Created
April 12, 2021 19:41
-
-
Save PadreSVK/3544f66553e5379cfaa9d84fed93def9 to your computer and use it in GitHub Desktop.
DBContext registration with design time factory and usage of extension method GetConnectionString
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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