Completely changes way of fetching context

This commit is contained in:
2020-01-22 22:09:07 +01:00
parent f28ea09235
commit cc8eb0302e
5 changed files with 69 additions and 35 deletions

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@ -21,37 +22,19 @@ namespace EntityFrameworkBasics
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
using(var context = new ApplicationDBContext())
{
context.Database.EnsureCreated();
if (!context.Settings.Any())
{
context.Settings.Add(new SettingsDataModel
{
Name = "BackgroundColor",
Value = "Red"
});
var SettingsLocally = context.Settings.Local.Count();
var SettingsDatabase = context.Settings.Count();
var firstLocal = context.Settings.Local.FirstOrDefault();
var firstDatabase = context.Settings.FirstOrDefault();
context.SaveChanges();
}
}
// Add ApplicationDbContext to DI
services.AddDbContext<ApplicationDBContext>(options =>
options.UseSqlServer("Server=192.168.0.135;Database=entityframework;User ID=sa;Password=SAtfoubuntu1SA;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;MultipleActiveResultSets=true;"));
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)
{
// Store Instance of the DI service provider so our application can access it anywhere
IocContainer.Provider = (ServiceProvider)serviceProvider;
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();
else