diff --git a/ASP.Net Core/EntityFrameworkBasics/Controllers/HomeController.cs b/ASP.Net Core/EntityFrameworkBasics/Controllers/HomeController.cs new file mode 100644 index 0000000..344c510 --- /dev/null +++ b/ASP.Net Core/EntityFrameworkBasics/Controllers/HomeController.cs @@ -0,0 +1,64 @@ +using Microsoft.AspNetCore.Mvc; +using System.Linq; + +namespace EntityFrameworkBasics.Controllers +{ + public class HomeController : Controller + { + #region Protected Members + + /// + /// The scoped Application context + /// + protected ApplicationDbContext mContext; + + #endregion + + #region Constructor + + /// + /// Default constructor + /// + /// The injected context + public HomeController(ApplicationDbContext context) + { + mContext = context; + } + + #endregion + + public IActionResult Index() + { + // Make sure we have the database + mContext.Database.EnsureCreated(); + + // If we have no settings already... + if (!mContext.Settings.Any()) + { + // Add a new setting + mContext.Settings.Add(new SettingsDataModel + { + Name = "BackgroundColor", + Value = "Red" + }); + + // Check to show the new setting is currently only local and not in the database + var settingsLocally = mContext.Settings.Local.Count(); + var settingsDatabase = mContext.Settings.Count(); + var firstLocal = mContext.Settings.Local.FirstOrDefault(); + var firstDatabase = mContext.Settings.FirstOrDefault(); + + // Commit setting to database + mContext.SaveChanges(); + + // Recheck to show its now in local and the actual database + settingsLocally = mContext.Settings.Local.Count(); + settingsDatabase = mContext.Settings.Count(); + firstLocal = mContext.Settings.Local.FirstOrDefault(); + firstDatabase = mContext.Settings.FirstOrDefault(); + } + + return View(); + } + } +} diff --git a/ASP.Net Core/EntityFrameworkBasics/Data/ApplicationDbContext.cs b/ASP.Net Core/EntityFrameworkBasics/Data/ApplicationDbContext.cs new file mode 100644 index 0000000..9741163 --- /dev/null +++ b/ASP.Net Core/EntityFrameworkBasics/Data/ApplicationDbContext.cs @@ -0,0 +1,42 @@ +using Microsoft.EntityFrameworkCore; +using System; + +namespace EntityFrameworkBasics +{ + /// + /// The database representational model for our application + /// + public class ApplicationDbContext : DbContext + { + #region Public Properties + + /// + /// The settings for the application + /// + public DbSet Settings { get; set; } + + #endregion + + #region Constructor + + /// + /// Default constructor, expecting database options passed in + /// + /// The database context options + public ApplicationDbContext(DbContextOptions options) : base(options) + { + + } + + #endregion + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + // Fluent API + + modelBuilder.Entity().HasIndex(a => a.Name); + } + } +} diff --git a/ASP.Net Core/EntityFrameworkBasics/Data/SettingsDataModel.cs b/ASP.Net Core/EntityFrameworkBasics/Data/SettingsDataModel.cs new file mode 100644 index 0000000..6f1c118 --- /dev/null +++ b/ASP.Net Core/EntityFrameworkBasics/Data/SettingsDataModel.cs @@ -0,0 +1,31 @@ +using System.ComponentModel.DataAnnotations; + +namespace EntityFrameworkBasics +{ + /// + /// Our Settings database table representational model + /// + public class SettingsDataModel + { + /// + /// The unique Id for this entry + /// + [Key] + public string Id { get; set; } + + /// + /// The settings name + /// + /// This column is indexed + [Required] + [MaxLength(256)] + public string Name { get; set; } + + /// + /// The settings value + /// + [Required] + [MaxLength(2048)] + public string Value { get; set; } + } +} diff --git a/ASP.Net Core/EntityFrameworkBasics/EntityFrameworkBasics.csproj b/ASP.Net Core/EntityFrameworkBasics/EntityFrameworkBasics.csproj new file mode 100644 index 0000000..64e2ad4 --- /dev/null +++ b/ASP.Net Core/EntityFrameworkBasics/EntityFrameworkBasics.csproj @@ -0,0 +1,24 @@ + + + + netcoreapp2.0 + AnyCPU + + + + + + + + + + + + + + + + + + + diff --git a/ASP.Net Core/EntityFrameworkBasics/EntityFrameworkBasics.sln b/ASP.Net Core/EntityFrameworkBasics/EntityFrameworkBasics.sln new file mode 100644 index 0000000..2bb9188 --- /dev/null +++ b/ASP.Net Core/EntityFrameworkBasics/EntityFrameworkBasics.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27130.2010 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkBasics", "EntityFrameworkBasics.csproj", "{B6C91DFF-E4A2-4DD3-A357-F8BECCCA183A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B6C91DFF-E4A2-4DD3-A357-F8BECCCA183A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B6C91DFF-E4A2-4DD3-A357-F8BECCCA183A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B6C91DFF-E4A2-4DD3-A357-F8BECCCA183A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B6C91DFF-E4A2-4DD3-A357-F8BECCCA183A}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {75575559-DA47-4D16-803A-735E85EACA3D} + EndGlobalSection +EndGlobal diff --git a/ASP.Net Core/EntityFrameworkBasics/IoC/IocContainer.cs b/ASP.Net Core/EntityFrameworkBasics/IoC/IocContainer.cs new file mode 100644 index 0000000..965ed06 --- /dev/null +++ b/ASP.Net Core/EntityFrameworkBasics/IoC/IocContainer.cs @@ -0,0 +1,27 @@ +using Microsoft.Extensions.DependencyInjection; +using System; + +namespace EntityFrameworkBasics +{ + /// + /// A shorthand access class to get DI services with nice clean short code + /// + public static class IoC + { + /// + /// The scoped instance of the + /// + 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/EntityFrameworkBasics/Program.cs b/ASP.Net Core/EntityFrameworkBasics/Program.cs new file mode 100644 index 0000000..82320c4 --- /dev/null +++ b/ASP.Net Core/EntityFrameworkBasics/Program.cs @@ -0,0 +1,21 @@ +using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Hosting; +using System.IO; + +namespace EntityFrameworkBasics +{ + public class Program + { + public static void Main(string[] args) + { + BuildWebHost(args).Run(); + } + + public static IWebHost BuildWebHost(string[] args) + { + return WebHost.CreateDefaultBuilder() + .UseStartup() + .Build(); + } + } +} diff --git a/ASP.Net Core/EntityFrameworkBasics/Properties/launchSettings.json b/ASP.Net Core/EntityFrameworkBasics/Properties/launchSettings.json new file mode 100644 index 0000000..806887c --- /dev/null +++ b/ASP.Net Core/EntityFrameworkBasics/Properties/launchSettings.json @@ -0,0 +1,12 @@ +{ + "profiles": { + "test": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "http://localhost:5000/" + } + } +} \ No newline at end of file diff --git a/ASP.Net Core/EntityFrameworkBasics/Startup.cs b/ASP.Net Core/EntityFrameworkBasics/Startup.cs new file mode 100644 index 0000000..ef430f3 --- /dev/null +++ b/ASP.Net Core/EntityFrameworkBasics/Startup.cs @@ -0,0 +1,55 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using System; + +namespace EntityFrameworkBasics +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + // Add ApplicationDbContext to DI + services.AddDbContext(options => + options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); + + 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, 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 + app.UseExceptionHandler("/Home/Error"); + + app.UseStaticFiles(); + + app.UseMvc(routes => + { + routes.MapRoute( + name: "default", + template: "{controller=Home}/{action=Index}/{moreInfo?}"); + + routes.MapRoute( + name: "aboutPage", + template: "more", + defaults: new { controller = "About", action = "TellMeMore" }); + }); + } + } +} diff --git a/ASP.Net Core/EntityFrameworkBasics/Views/Home/Index.cshtml b/ASP.Net Core/EntityFrameworkBasics/Views/Home/Index.cshtml new file mode 100644 index 0000000..4a830af --- /dev/null +++ b/ASP.Net Core/EntityFrameworkBasics/Views/Home/Index.cshtml @@ -0,0 +1 @@ +

Hello World

\ No newline at end of file diff --git a/ASP.Net Core/EntityFrameworkBasics/Views/Shared/_Layout.cshtml b/ASP.Net Core/EntityFrameworkBasics/Views/Shared/_Layout.cshtml new file mode 100644 index 0000000..553e3ee --- /dev/null +++ b/ASP.Net Core/EntityFrameworkBasics/Views/Shared/_Layout.cshtml @@ -0,0 +1,10 @@ + + + + + Title + + + @RenderBody() + + \ No newline at end of file diff --git a/ASP.Net Core/EntityFrameworkBasics/Views/_ViewImports.cshtml b/ASP.Net Core/EntityFrameworkBasics/Views/_ViewImports.cshtml new file mode 100644 index 0000000..a757b41 --- /dev/null +++ b/ASP.Net Core/EntityFrameworkBasics/Views/_ViewImports.cshtml @@ -0,0 +1 @@ +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers diff --git a/ASP.Net Core/EntityFrameworkBasics/Views/_ViewStart.cshtml b/ASP.Net Core/EntityFrameworkBasics/Views/_ViewStart.cshtml new file mode 100644 index 0000000..a5f1004 --- /dev/null +++ b/ASP.Net Core/EntityFrameworkBasics/Views/_ViewStart.cshtml @@ -0,0 +1,3 @@ +@{ + Layout = "_Layout"; +} diff --git a/ASP.Net Core/EntityFrameworkBasics/appsettings.Development.json b/ASP.Net Core/EntityFrameworkBasics/appsettings.Development.json new file mode 100644 index 0000000..fa8ce71 --- /dev/null +++ b/ASP.Net Core/EntityFrameworkBasics/appsettings.Development.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "IncludeScopes": false, + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + } +} diff --git a/ASP.Net Core/EntityFrameworkBasics/appsettings.json b/ASP.Net Core/EntityFrameworkBasics/appsettings.json new file mode 100644 index 0000000..68aa19e --- /dev/null +++ b/ASP.Net Core/EntityFrameworkBasics/appsettings.json @@ -0,0 +1,12 @@ +{ + "ConnectionStrings": { + "DefaultConnection": "Server=.;Database=entityframework;Trusted_Connection=True;MultipleActiveResultSets=true" + }, + + "Logging": { + "IncludeScopes": false, + "LogLevel": { + "Default": "Warning" + } + } +}