From 137417fde857d4aa739e0c50fb74d28801c060d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Wed, 22 Jan 2020 20:30:40 +0100 Subject: [PATCH] Database creates (entity framework) --- .../MVCBasics/Controllers/AboutController.cs | 2 +- .../MVCBasics/Controllers/HomeController.cs | 2 +- .../MVCBasics/Data/ApplicationDBContext.cs | 39 +++++++++++++++++++ .../MVCBasics/Data/SettingsDataModel.cs | 21 ++++++++++ ...st.csproj => EntityFrameworkBasics.csproj} | 0 ASP.Net Core/MVCBasics/Program.cs | 2 +- ASP.Net Core/MVCBasics/Startup.cs | 7 +++- .../MVCBasics/Views/_ViewImports.cshtml | 2 +- ASP.Net Core/MVCBasics/test.sln | 6 +-- 9 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 ASP.Net Core/MVCBasics/Data/ApplicationDBContext.cs create mode 100644 ASP.Net Core/MVCBasics/Data/SettingsDataModel.cs rename ASP.Net Core/MVCBasics/{test.csproj => EntityFrameworkBasics.csproj} (100%) diff --git a/ASP.Net Core/MVCBasics/Controllers/AboutController.cs b/ASP.Net Core/MVCBasics/Controllers/AboutController.cs index 2ed65a6..c2fd469 100644 --- a/ASP.Net Core/MVCBasics/Controllers/AboutController.cs +++ b/ASP.Net Core/MVCBasics/Controllers/AboutController.cs @@ -1,6 +1,6 @@ using Microsoft.AspNetCore.Mvc; -namespace test.Controllers +namespace EntityFrameworkBasics.Controllers { public class AboutController : Controller { diff --git a/ASP.Net Core/MVCBasics/Controllers/HomeController.cs b/ASP.Net Core/MVCBasics/Controllers/HomeController.cs index cdc156c..462180b 100644 --- a/ASP.Net Core/MVCBasics/Controllers/HomeController.cs +++ b/ASP.Net Core/MVCBasics/Controllers/HomeController.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.Mvc; using System; -namespace test.Controllers +namespace EntityFrameworkBasics.Controllers { public class HomeController : Controller { diff --git a/ASP.Net Core/MVCBasics/Data/ApplicationDBContext.cs b/ASP.Net Core/MVCBasics/Data/ApplicationDBContext.cs new file mode 100644 index 0000000..ddd47e1 --- /dev/null +++ b/ASP.Net Core/MVCBasics/Data/ApplicationDBContext.cs @@ -0,0 +1,39 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EntityFrameworkBasics +{ + /// + /// The database representational model for application + /// + public class ApplicationDBContext : DbContext + { + #region Public properties + + public DbSet Settings { get; set; } + + #endregion + + public ApplicationDBContext() + { + + } + + 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;"); + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + } + } +} diff --git a/ASP.Net Core/MVCBasics/Data/SettingsDataModel.cs b/ASP.Net Core/MVCBasics/Data/SettingsDataModel.cs new file mode 100644 index 0000000..196736f --- /dev/null +++ b/ASP.Net Core/MVCBasics/Data/SettingsDataModel.cs @@ -0,0 +1,21 @@ +namespace EntityFrameworkBasics +{ + /// + /// Our Settings database table representional model + /// + public class SettingsDataModel + { + /// + /// The unique Id for this entry + /// + public string Id { get; set; } + /// + /// The settings name + /// + public string Name { get; set; } + /// + /// The settings value + /// + public string Value { get; set; } + } +} diff --git a/ASP.Net Core/MVCBasics/test.csproj b/ASP.Net Core/MVCBasics/EntityFrameworkBasics.csproj similarity index 100% rename from ASP.Net Core/MVCBasics/test.csproj rename to ASP.Net Core/MVCBasics/EntityFrameworkBasics.csproj diff --git a/ASP.Net Core/MVCBasics/Program.cs b/ASP.Net Core/MVCBasics/Program.cs index 65b46d0..4e98cab 100644 --- a/ASP.Net Core/MVCBasics/Program.cs +++ b/ASP.Net Core/MVCBasics/Program.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.Hosting; using System.IO; -namespace test +namespace EntityFrameworkBasics { public class Program { diff --git a/ASP.Net Core/MVCBasics/Startup.cs b/ASP.Net Core/MVCBasics/Startup.cs index df6aabc..7ceb72a 100644 --- a/ASP.Net Core/MVCBasics/Startup.cs +++ b/ASP.Net Core/MVCBasics/Startup.cs @@ -7,7 +7,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -namespace test +namespace EntityFrameworkBasics { public class Startup { @@ -21,6 +21,11 @@ namespace test // 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(); + } + services.AddMvc(); } diff --git a/ASP.Net Core/MVCBasics/Views/_ViewImports.cshtml b/ASP.Net Core/MVCBasics/Views/_ViewImports.cshtml index c9d2285..f547c19 100644 --- a/ASP.Net Core/MVCBasics/Views/_ViewImports.cshtml +++ b/ASP.Net Core/MVCBasics/Views/_ViewImports.cshtml @@ -1,2 +1,2 @@ -@using test +@using EntityFrameworkBasics @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers diff --git a/ASP.Net Core/MVCBasics/test.sln b/ASP.Net Core/MVCBasics/test.sln index 7094101..dadab2a 100644 --- a/ASP.Net Core/MVCBasics/test.sln +++ b/ASP.Net Core/MVCBasics/test.sln @@ -1,9 +1,9 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.27130.2010 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29509.3 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "test", "test.csproj", "{B6C91DFF-E4A2-4DD3-A357-F8BECCCA183A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkBasics", "EntityFrameworkBasics.csproj", "{B6C91DFF-E4A2-4DD3-A357-F8BECCCA183A}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution