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

@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System; using System;
using System.Linq;
namespace EntityFrameworkBasics.Controllers namespace EntityFrameworkBasics.Controllers
{ {
@ -7,6 +8,28 @@ namespace EntityFrameworkBasics.Controllers
{ {
public IActionResult Index() 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(); return View();
} }

View File

@ -11,24 +11,26 @@ namespace EntityFrameworkBasics
/// </summary> /// </summary>
public class ApplicationDBContext : DbContext public class ApplicationDBContext : DbContext
{ {
public string Id { get; set; } = Guid.NewGuid().ToString("N");
#region Public properties #region Public properties
public DbSet<SettingsDataModel> Settings { get; set; } public DbSet<SettingsDataModel> Settings { get; set; }
#endregion #endregion
public ApplicationDBContext() #region Constructor
/// <summary>
/// Default constructor, expecting database options passed in
/// </summary>
/// <param name="options">The database context options</param>
public ApplicationDBContext(DbContextOptions<ApplicationDBContext> options) : base(options)
{ {
} }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) #endregion
{
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;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
{ {

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework> <TargetFramework>netcoreapp2.0</TargetFramework>

View File

@ -0,0 +1,26 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace EntityFrameworkBasics
{
/// <summary>
/// A shorthand access class to get DI services with nice clean short code
/// </summary>
public static class IoC
{
public static ApplicationDBContext ApplicationDbContext => IocContainer.Provider.GetService<ApplicationDBContext>();
}
/// <summary>
/// The dependency Injection container making use of the built in .Net Core service provider
/// </summary>
public static class IocContainer
{
/// <summary>
/// The service provider for this application
/// </summary>
public static ServiceProvider Provider { get; set; }
}
}

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; 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. // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
using(var context = new ApplicationDBContext()) // Add ApplicationDbContext to DI
{ services.AddDbContext<ApplicationDBContext>(options =>
context.Database.EnsureCreated(); options.UseSqlServer("Server=192.168.0.135;Database=entityframework;User ID=sa;Password=SAtfoubuntu1SA;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;MultipleActiveResultSets=true;"));
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();
}
}
services.AddMvc(); services.AddMvc();
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // 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()) if (env.IsDevelopment())
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
else else