From 96a2112671b4724e791f3d84bb6d846b4d611f1a Mon Sep 17 00:00:00 2001 From: Tim Cliff Date: Fri, 3 Jun 2022 07:37:36 +0100 Subject: [PATCH] Add initial database context and first entity --- Component/Entry/Repository/Entry.cs | 29 ++++++++++ .../Entry/Repository/EntryConfiguration.cs | 13 +++++ Data/DiaryDBContext.cs | 27 +++++++++ .../20220603063002_Initial.Designer.cs | 55 +++++++++++++++++++ Data/Migrations/20220603063002_Initial.cs | 40 ++++++++++++++ .../Migrations/DiaryDBContextModelSnapshot.cs | 53 ++++++++++++++++++ Diary.csproj | 8 +++ Program.cs | 36 ++++++++++-- Properties/launchSettings.json | 19 +------ appsettings.json | 6 ++ 10 files changed, 265 insertions(+), 21 deletions(-) create mode 100644 Component/Entry/Repository/Entry.cs create mode 100644 Component/Entry/Repository/EntryConfiguration.cs create mode 100644 Data/DiaryDBContext.cs create mode 100644 Data/Migrations/20220603063002_Initial.Designer.cs create mode 100644 Data/Migrations/20220603063002_Initial.cs create mode 100644 Data/Migrations/DiaryDBContextModelSnapshot.cs diff --git a/Component/Entry/Repository/Entry.cs b/Component/Entry/Repository/Entry.cs new file mode 100644 index 0000000..dd2a7de --- /dev/null +++ b/Component/Entry/Repository/Entry.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Diary.Component.Entry.Repository +{ + [Table("Entries", Schema = "Diary")] + public class Entry + { + [Key] + public int EnrtyID { get; set; } + + [Required] + [Column(TypeName="Date")] + public DateTime Date { get; set; } + + [Required] + public DateTime ValidFrom { get; set; } + public DateTime? ValidTo { get; set; } + + public string Note { get; set; } + + + } +} diff --git a/Component/Entry/Repository/EntryConfiguration.cs b/Component/Entry/Repository/EntryConfiguration.cs new file mode 100644 index 0000000..0512f2d --- /dev/null +++ b/Component/Entry/Repository/EntryConfiguration.cs @@ -0,0 +1,13 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace Diary.Component.Entry.Repository +{ + public class EntryConfiguration : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + + } + } +} diff --git a/Data/DiaryDBContext.cs b/Data/DiaryDBContext.cs new file mode 100644 index 0000000..a62d688 --- /dev/null +++ b/Data/DiaryDBContext.cs @@ -0,0 +1,27 @@ +using Diary.Component.Entry.Repository; +using Microsoft.EntityFrameworkCore; + +namespace Diary.Data +{ + + public interface IDiaryDBContext + { + DbSet Entries { get; set; } + } + + public class DiaryDBContext : DbContext, IDiaryDBContext + { + public DbSet Entries { get; set; } + + public DiaryDBContext(DbContextOptions options) : base(options) + { + } + + protected override void OnModelCreating(ModelBuilder builder) + { + base.OnModelCreating(builder); + + builder.ApplyConfiguration(new EntryConfiguration()); + } + } +} \ No newline at end of file diff --git a/Data/Migrations/20220603063002_Initial.Designer.cs b/Data/Migrations/20220603063002_Initial.Designer.cs new file mode 100644 index 0000000..ac28481 --- /dev/null +++ b/Data/Migrations/20220603063002_Initial.Designer.cs @@ -0,0 +1,55 @@ +// +using System; +using Diary.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace Diary.Data.Migrations +{ + [DbContext(typeof(DiaryDBContext))] + [Migration("20220603063002_Initial")] + partial class Initial + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.5") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); + + modelBuilder.Entity("Diary.Component.Entry.Repository.Entry", b => + { + b.Property("EnrtyID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("EnrtyID"), 1L, 1); + + b.Property("Date") + .HasColumnType("Date"); + + b.Property("Note") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ValidFrom") + .HasColumnType("datetime2"); + + b.Property("ValidTo") + .HasColumnType("datetime2"); + + b.HasKey("EnrtyID"); + + b.ToTable("Entries", "Diary"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Data/Migrations/20220603063002_Initial.cs b/Data/Migrations/20220603063002_Initial.cs new file mode 100644 index 0000000..d62f13e --- /dev/null +++ b/Data/Migrations/20220603063002_Initial.cs @@ -0,0 +1,40 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Diary.Data.Migrations +{ + public partial class Initial : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.EnsureSchema( + name: "Diary"); + + migrationBuilder.CreateTable( + name: "Entries", + schema: "Diary", + columns: table => new + { + EnrtyID = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Date = table.Column(type: "Date", nullable: false), + ValidFrom = table.Column(type: "datetime2", nullable: false), + ValidTo = table.Column(type: "datetime2", nullable: true), + Note = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Entries", x => x.EnrtyID); + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Entries", + schema: "Diary"); + } + } +} diff --git a/Data/Migrations/DiaryDBContextModelSnapshot.cs b/Data/Migrations/DiaryDBContextModelSnapshot.cs new file mode 100644 index 0000000..28b256e --- /dev/null +++ b/Data/Migrations/DiaryDBContextModelSnapshot.cs @@ -0,0 +1,53 @@ +// +using System; +using Diary.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace Diary.Data.Migrations +{ + [DbContext(typeof(DiaryDBContext))] + partial class DiaryDBContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.5") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); + + modelBuilder.Entity("Diary.Component.Entry.Repository.Entry", b => + { + b.Property("EnrtyID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("EnrtyID"), 1L, 1); + + b.Property("Date") + .HasColumnType("Date"); + + b.Property("Note") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ValidFrom") + .HasColumnType("datetime2"); + + b.Property("ValidTo") + .HasColumnType("datetime2"); + + b.HasKey("EnrtyID"); + + b.ToTable("Entries", "Diary"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Diary.csproj b/Diary.csproj index 60bf9ea..91ff7c5 100644 --- a/Diary.csproj +++ b/Diary.csproj @@ -7,6 +7,14 @@ + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/Program.cs b/Program.cs index 48863a6..17addf3 100644 --- a/Program.cs +++ b/Program.cs @@ -1,8 +1,31 @@ +using Diary.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; + var builder = WebApplication.CreateBuilder(args); -// Add services to the container. +//GetConfigStuff +IConfigurationRoot? config = new ConfigurationBuilder() + .AddJsonFile("appsettings.json",false) + .Build(); + + + + +builder.Services + .AddDbContext(opt => + { + string connectionString = config.GetConnectionString("Diary"); + opt.UseSqlServer(connectionString); + }); + + +// Add services to the container. +builder.Services.AddDbContext(); builder.Services.AddControllers(); +builder.Services.AddScoped(); + // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); @@ -12,8 +35,13 @@ var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { - app.UseSwagger(); - app.UseSwaggerUI(); + app.UseSwagger(); + app.UseSwaggerUI(opt => + { + opt.SwaggerEndpoint("/swagger/v1/swagger.json", "v1"); + opt.RoutePrefix = String.Empty; + } + ); } app.UseHttpsRedirection(); @@ -22,4 +50,4 @@ app.UseAuthorization(); app.MapControllers(); -app.Run(); +app.Run(); \ No newline at end of file diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json index f86452f..1f1305a 100644 --- a/Properties/launchSettings.json +++ b/Properties/launchSettings.json @@ -1,28 +1,13 @@ { "$schema": "https://json.schemastore.org/launchsettings.json", - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:10709", - "sslPort": 44301 - } - }, + "profiles": { "Diary": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, "launchUrl": "swagger", - "applicationUrl": "https://localhost:7252;http://localhost:5286", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "launchUrl": "swagger", + "applicationUrl": "https://localhost:5001", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/appsettings.json b/appsettings.json index 10f68b8..b97fc1f 100644 --- a/appsettings.json +++ b/appsettings.json @@ -1,4 +1,10 @@ { + "Connectionstrings": + { + "Diary":"Server=192.168.1.20;Database=Dev_Diary;User Id=sa;Password=P@$$W0rd2021!.;" + }, + + "Logging": { "LogLevel": { "Default": "Information",