From cc8eb0302e843a240dd9855aea7e906721dd89f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Wed, 22 Jan 2020 22:09:07 +0100 Subject: [PATCH] Completely changes way of fetching context --- .../MVCBasics/Controllers/HomeController.cs | 23 ++++++++++++ .../MVCBasics/Data/ApplicationDBContext.cs | 18 +++++----- .../MVCBasics/EntityFrameworkBasics.csproj | 2 +- ASP.Net Core/MVCBasics/IoC/IocContainer.cs | 26 ++++++++++++++ ASP.Net Core/MVCBasics/Startup.cs | 35 +++++-------------- 5 files changed, 69 insertions(+), 35 deletions(-) create mode 100644 ASP.Net Core/MVCBasics/IoC/IocContainer.cs diff --git a/ASP.Net Core/MVCBasics/Controllers/HomeController.cs b/ASP.Net Core/MVCBasics/Controllers/HomeController.cs index 462180b..c7d8cd6 100644 --- a/ASP.Net Core/MVCBasics/Controllers/HomeController.cs +++ b/ASP.Net Core/MVCBasics/Controllers/HomeController.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Mvc; using System; +using System.Linq; namespace EntityFrameworkBasics.Controllers { @@ -7,6 +8,28 @@ namespace EntityFrameworkBasics.Controllers { public IActionResult Index() { + // No using statement because of the DI and IoC + var context = IoC.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(); + } + return View(); } diff --git a/ASP.Net Core/MVCBasics/Data/ApplicationDBContext.cs b/ASP.Net Core/MVCBasics/Data/ApplicationDBContext.cs index ddd47e1..7d0e18d 100644 --- a/ASP.Net Core/MVCBasics/Data/ApplicationDBContext.cs +++ b/ASP.Net Core/MVCBasics/Data/ApplicationDBContext.cs @@ -11,24 +11,26 @@ namespace EntityFrameworkBasics /// public class ApplicationDBContext : DbContext { + + public string Id { get; set; } = Guid.NewGuid().ToString("N"); + #region Public properties public DbSet Settings { get; set; } #endregion - public ApplicationDBContext() + #region Constructor + /// + /// Default constructor, expecting database options passed in + /// + /// The database context options + public ApplicationDBContext(DbContextOptions options) : base(options) { } - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - base.OnConfiguring(optionsBuilder); - // tfoUbuntu;User ID=sa;Password=********;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False - //optionsBuilder.UseSqlServer("Server=192.168.0.135;Database=entityframework;") - optionsBuilder.UseSqlServer("Server=192.168.0.135;Database=entityframework;User ID=sa;Password=SAtfoubuntu1SA;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;MultipleActiveResultSets=true;"); - } + #endregion protected override void OnModelCreating(ModelBuilder modelBuilder) { diff --git a/ASP.Net Core/MVCBasics/EntityFrameworkBasics.csproj b/ASP.Net Core/MVCBasics/EntityFrameworkBasics.csproj index 74ef3a5..97279c7 100644 --- a/ASP.Net Core/MVCBasics/EntityFrameworkBasics.csproj +++ b/ASP.Net Core/MVCBasics/EntityFrameworkBasics.csproj @@ -1,4 +1,4 @@ - + netcoreapp2.0 diff --git a/ASP.Net Core/MVCBasics/IoC/IocContainer.cs b/ASP.Net Core/MVCBasics/IoC/IocContainer.cs new file mode 100644 index 0000000..75c9361 --- /dev/null +++ b/ASP.Net Core/MVCBasics/IoC/IocContainer.cs @@ -0,0 +1,26 @@ +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EntityFrameworkBasics +{ + /// + /// A shorthand access class to get DI services with nice clean short code + /// + public static class IoC + { + public static ApplicationDBContext ApplicationDbContext => IocContainer.Provider.GetService(); + } + /// + /// The dependency Injection container making use of the built in .Net Core service provider + /// + public static class IocContainer + { + /// + /// The service provider for this application + /// + public static ServiceProvider Provider { get; set; } + } +} diff --git a/ASP.Net Core/MVCBasics/Startup.cs b/ASP.Net Core/MVCBasics/Startup.cs index 9e03c75..77a53dd 100644 --- a/ASP.Net Core/MVCBasics/Startup.cs +++ b/ASP.Net Core/MVCBasics/Startup.cs @@ -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(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