Completely changes way of fetching context
This commit is contained in:
@ -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();
|
||||
}
|
||||
|
||||
|
||||
@ -11,24 +11,26 @@ namespace EntityFrameworkBasics
|
||||
/// </summary>
|
||||
public class ApplicationDBContext : DbContext
|
||||
{
|
||||
|
||||
public string Id { get; set; } = Guid.NewGuid().ToString("N");
|
||||
|
||||
#region Public properties
|
||||
|
||||
public DbSet<SettingsDataModel> Settings { get; set; }
|
||||
|
||||
#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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
|
||||
26
ASP.Net Core/MVCBasics/IoC/IocContainer.cs
Normal file
26
ASP.Net Core/MVCBasics/IoC/IocContainer.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user