Entity Framework Basics
This commit is contained in:
@ -0,0 +1,64 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Linq;
|
||||
|
||||
namespace EntityFrameworkBasics.Controllers
|
||||
{
|
||||
public class HomeController : Controller
|
||||
{
|
||||
#region Protected Members
|
||||
|
||||
/// <summary>
|
||||
/// The scoped Application context
|
||||
/// </summary>
|
||||
protected ApplicationDbContext mContext;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>
|
||||
/// Default constructor
|
||||
/// </summary>
|
||||
/// <param name="context">The injected context</param>
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,42 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
|
||||
namespace EntityFrameworkBasics
|
||||
{
|
||||
/// <summary>
|
||||
/// The database representational model for our application
|
||||
/// </summary>
|
||||
public class ApplicationDbContext : DbContext
|
||||
{
|
||||
#region Public Properties
|
||||
|
||||
/// <summary>
|
||||
/// The settings for the application
|
||||
/// </summary>
|
||||
public DbSet<SettingsDataModel> Settings { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#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)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
// Fluent API
|
||||
|
||||
modelBuilder.Entity<SettingsDataModel>().HasIndex(a => a.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
31
ASP.Net Core/EntityFrameworkBasics/Data/SettingsDataModel.cs
Normal file
31
ASP.Net Core/EntityFrameworkBasics/Data/SettingsDataModel.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace EntityFrameworkBasics
|
||||
{
|
||||
/// <summary>
|
||||
/// Our Settings database table representational model
|
||||
/// </summary>
|
||||
public class SettingsDataModel
|
||||
{
|
||||
/// <summary>
|
||||
/// The unique Id for this entry
|
||||
/// </summary>
|
||||
[Key]
|
||||
public string Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The settings name
|
||||
/// </summary>
|
||||
/// <remarks>This column is indexed</remarks>
|
||||
[Required]
|
||||
[MaxLength(256)]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The settings value
|
||||
/// </summary>
|
||||
[Required]
|
||||
[MaxLength(2048)]
|
||||
public string Value { get; set; }
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
<Platforms>AnyCPU</Platforms>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Remove="wwwroot\style.xaml" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="wwwroot\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
25
ASP.Net Core/EntityFrameworkBasics/EntityFrameworkBasics.sln
Normal file
25
ASP.Net Core/EntityFrameworkBasics/EntityFrameworkBasics.sln
Normal file
@ -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
|
||||
27
ASP.Net Core/EntityFrameworkBasics/IoC/IocContainer.cs
Normal file
27
ASP.Net Core/EntityFrameworkBasics/IoC/IocContainer.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
|
||||
namespace EntityFrameworkBasics
|
||||
{
|
||||
/// <summary>
|
||||
/// A shorthand access class to get DI services with nice clean short code
|
||||
/// </summary>
|
||||
public static class IoC
|
||||
{
|
||||
/// <summary>
|
||||
/// The scoped instance of the <see cref="ApplicationDbContext"/>
|
||||
/// </summary>
|
||||
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; }
|
||||
}
|
||||
}
|
||||
21
ASP.Net Core/EntityFrameworkBasics/Program.cs
Normal file
21
ASP.Net Core/EntityFrameworkBasics/Program.cs
Normal file
@ -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<Startup>()
|
||||
.Build();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
{
|
||||
"profiles": {
|
||||
"test": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"applicationUrl": "http://localhost:5000/"
|
||||
}
|
||||
}
|
||||
}
|
||||
55
ASP.Net Core/EntityFrameworkBasics/Startup.cs
Normal file
55
ASP.Net Core/EntityFrameworkBasics/Startup.cs
Normal file
@ -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<ApplicationDbContext>(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" });
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
<h1>Hello World</h1>
|
||||
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en-GB">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
@RenderBody()
|
||||
</body>
|
||||
</html>
|
||||
@ -0,0 +1 @@
|
||||
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
|
||||
@ -0,0 +1,3 @@
|
||||
@{
|
||||
Layout = "_Layout";
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
{
|
||||
"Logging": {
|
||||
"IncludeScopes": false,
|
||||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
"System": "Information",
|
||||
"Microsoft": "Information"
|
||||
}
|
||||
}
|
||||
}
|
||||
12
ASP.Net Core/EntityFrameworkBasics/appsettings.json
Normal file
12
ASP.Net Core/EntityFrameworkBasics/appsettings.json
Normal file
@ -0,0 +1,12 @@
|
||||
{
|
||||
"ConnectionStrings": {
|
||||
"DefaultConnection": "Server=.;Database=entityframework;Trusted_Connection=True;MultipleActiveResultSets=true"
|
||||
},
|
||||
|
||||
"Logging": {
|
||||
"IncludeScopes": false,
|
||||
"LogLevel": {
|
||||
"Default": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user