From 8923b725c2ae81a3197424ceef09fd6bed203cd4 Mon Sep 17 00:00:00 2001 From: Nick Rirush Date: Tue, 5 Feb 2019 17:44:55 +0800 Subject: [PATCH] Add database context, User type and migrations --- Migrations/20190205093645_CreateUsers.Designer.cs | 49 +++++++++++++++++++++++ Migrations/20190205093645_CreateUsers.cs | 35 ++++++++++++++++ Migrations/DatabaseContextModelSnapshot.cs | 47 ++++++++++++++++++++++ Pages/Index.cshtml | 2 + Pages/_Layout.cshtml | 7 ++++ Pages/_ViewImports.cshtml | 0 Pages/_ViewStart.cshtml | 3 ++ QuickCHAT.csproj | 1 + Source/Model.cs | 30 ++++++++++++++ Source/Startup.cs | 25 +++++++++--- appsettings.Development.json | 3 ++ 11 files changed, 197 insertions(+), 5 deletions(-) create mode 100644 Migrations/20190205093645_CreateUsers.Designer.cs create mode 100644 Migrations/20190205093645_CreateUsers.cs create mode 100644 Migrations/DatabaseContextModelSnapshot.cs create mode 100644 Pages/Index.cshtml create mode 100644 Pages/_Layout.cshtml create mode 100644 Pages/_ViewImports.cshtml create mode 100644 Pages/_ViewStart.cshtml create mode 100644 Source/Model.cs diff --git a/Migrations/20190205093645_CreateUsers.Designer.cs b/Migrations/20190205093645_CreateUsers.Designer.cs new file mode 100644 index 0000000..bc71041 --- /dev/null +++ b/Migrations/20190205093645_CreateUsers.Designer.cs @@ -0,0 +1,49 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using QuickCHAT.Models; + +namespace QuickCHAT.Migrations +{ + [DbContext(typeof(DatabaseContext))] + [Migration("20190205093645_CreateUsers")] + partial class CreateUsers + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn) + .HasAnnotation("ProductVersion", "2.2.1-servicing-10028") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + modelBuilder.Entity("QuickCHAT.Models.User", b => + { + b.Property("UserID") + .ValueGeneratedOnAdd(); + + b.Property("Description"); + + b.Property("First"); + + b.Property("Last"); + + b.Property("Password"); + + b.Property("Username") + .IsRequired(); + + b.HasKey("UserID"); + + b.HasAlternateKey("Username") + .HasName("AK_Username"); + + b.ToTable("Users"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20190205093645_CreateUsers.cs b/Migrations/20190205093645_CreateUsers.cs new file mode 100644 index 0000000..6e9b3f2 --- /dev/null +++ b/Migrations/20190205093645_CreateUsers.cs @@ -0,0 +1,35 @@ +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace QuickCHAT.Migrations +{ + public partial class CreateUsers : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Users", + columns: table => new + { + UserID = table.Column(nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn), + Username = table.Column(nullable: false), + Password = table.Column(nullable: true), + First = table.Column(nullable: true), + Last = table.Column(nullable: true), + Description = table.Column(nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Users", x => x.UserID); + table.UniqueConstraint("AK_Username", x => x.Username); + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Users"); + } + } +} diff --git a/Migrations/DatabaseContextModelSnapshot.cs b/Migrations/DatabaseContextModelSnapshot.cs new file mode 100644 index 0000000..4a717ff --- /dev/null +++ b/Migrations/DatabaseContextModelSnapshot.cs @@ -0,0 +1,47 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using QuickCHAT.Models; + +namespace QuickCHAT.Migrations +{ + [DbContext(typeof(DatabaseContext))] + partial class DatabaseContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn) + .HasAnnotation("ProductVersion", "2.2.1-servicing-10028") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + modelBuilder.Entity("QuickCHAT.Models.User", b => + { + b.Property("UserID") + .ValueGeneratedOnAdd(); + + b.Property("Description"); + + b.Property("First"); + + b.Property("Last"); + + b.Property("Password"); + + b.Property("Username") + .IsRequired(); + + b.HasKey("UserID"); + + b.HasAlternateKey("Username") + .HasName("AK_Username"); + + b.ToTable("Users"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Pages/Index.cshtml b/Pages/Index.cshtml new file mode 100644 index 0000000..49de0f2 --- /dev/null +++ b/Pages/Index.cshtml @@ -0,0 +1,2 @@ +@page +

Hello, World!

\ No newline at end of file diff --git a/Pages/_Layout.cshtml b/Pages/_Layout.cshtml new file mode 100644 index 0000000..103a8d6 --- /dev/null +++ b/Pages/_Layout.cshtml @@ -0,0 +1,7 @@ + + + @ViewData["Title"] - QuickCHAT + + + @RenderBody() + \ No newline at end of file diff --git a/Pages/_ViewImports.cshtml b/Pages/_ViewImports.cshtml new file mode 100644 index 0000000..e69de29 diff --git a/Pages/_ViewStart.cshtml b/Pages/_ViewStart.cshtml new file mode 100644 index 0000000..d641c67 --- /dev/null +++ b/Pages/_ViewStart.cshtml @@ -0,0 +1,3 @@ +@{ + Layout = "_Layout"; +} \ No newline at end of file diff --git a/QuickCHAT.csproj b/QuickCHAT.csproj index 423afac..a76fb81 100644 --- a/QuickCHAT.csproj +++ b/QuickCHAT.csproj @@ -8,6 +8,7 @@ + diff --git a/Source/Model.cs b/Source/Model.cs new file mode 100644 index 0000000..4c1b2d2 --- /dev/null +++ b/Source/Model.cs @@ -0,0 +1,30 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; + +namespace QuickCHAT.Models { + public class DatabaseContext : DbContext { + public DbSet Users { get; set; } + + public DatabaseContext(DbContextOptions options) : base(options) + {} + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { + + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) { + modelBuilder.Entity() + .HasAlternateKey(u => u.Username) + .HasName("AK_Username"); + } + } + + public class User { + public int UserID { get; set; } + public string Username { get; set; } + public string Password { get; set; } + public string First { get; set; } + public string Last { get; set; } + public string Description { get; set; } + } +} \ No newline at end of file diff --git a/Source/Startup.cs b/Source/Startup.cs index 54547b2..31e5268 100644 --- a/Source/Startup.cs +++ b/Source/Startup.cs @@ -5,16 +5,31 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using QuickCHAT.Models; namespace QuickCHAT { public class Startup { + public IConfiguration Configuration { get; } + + public Startup(IConfiguration configuration) { + Configuration = configuration; + } + // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { + services.AddMvc(); + services.AddEntityFrameworkNpgsql() + .AddDbContext( + options => options.UseNpgsql(Configuration.GetConnectionString("MainDatabase")) + ) + .BuildServiceProvider(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -23,12 +38,12 @@ namespace QuickCHAT if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); + } else { + app.UseHsts(); + app.UseHttpsRedirection(); } - - app.Run(async (context) => - { - await context.Response.WriteAsync("Hello World!"); - }); + + app.UseMvc(); } } } diff --git a/appsettings.Development.json b/appsettings.Development.json index e203e94..fed5ed4 100644 --- a/appsettings.Development.json +++ b/appsettings.Development.json @@ -5,5 +5,8 @@ "System": "Information", "Microsoft": "Information" } + }, + "ConnectionStrings": { + "MainDatabase": "Host=localhost;Database=quickchat;Username=quickchat;Password=quickchat" } }