Add initial database context and first entity

This commit is contained in:
2022-06-03 07:37:36 +01:00
parent 54bcf46cd9
commit 96a2112671
10 changed files with 265 additions and 21 deletions

View File

@@ -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; }
}
}

View File

@@ -0,0 +1,13 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Diary.Component.Entry.Repository
{
public class EntryConfiguration : IEntityTypeConfiguration<Entry>
{
public void Configure(EntityTypeBuilder<Entry> builder)
{
}
}
}

27
Data/DiaryDBContext.cs Normal file
View File

@@ -0,0 +1,27 @@
using Diary.Component.Entry.Repository;
using Microsoft.EntityFrameworkCore;
namespace Diary.Data
{
public interface IDiaryDBContext
{
DbSet<Entry> Entries { get; set; }
}
public class DiaryDBContext : DbContext, IDiaryDBContext
{
public DbSet<Entry> Entries { get; set; }
public DiaryDBContext(DbContextOptions<DiaryDBContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.ApplyConfiguration(new EntryConfiguration());
}
}
}

View File

@@ -0,0 +1,55 @@
// <auto-generated />
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<int>("EnrtyID")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("EnrtyID"), 1L, 1);
b.Property<DateTime>("Date")
.HasColumnType("Date");
b.Property<string>("Note")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("ValidFrom")
.HasColumnType("datetime2");
b.Property<DateTime?>("ValidTo")
.HasColumnType("datetime2");
b.HasKey("EnrtyID");
b.ToTable("Entries", "Diary");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -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<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Date = table.Column<DateTime>(type: "Date", nullable: false),
ValidFrom = table.Column<DateTime>(type: "datetime2", nullable: false),
ValidTo = table.Column<DateTime>(type: "datetime2", nullable: true),
Note = table.Column<string>(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");
}
}
}

View File

@@ -0,0 +1,53 @@
// <auto-generated />
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<int>("EnrtyID")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("EnrtyID"), 1L, 1);
b.Property<DateTime>("Date")
.HasColumnType("Date");
b.Property<string>("Note")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("ValidFrom")
.HasColumnType("datetime2");
b.Property<DateTime?>("ValidTo")
.HasColumnType("datetime2");
b.HasKey("EnrtyID");
b.ToTable("Entries", "Diary");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -7,6 +7,14 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="AutoMapper" Version="11.0.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="11.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup> </ItemGroup>

View File

@@ -1,8 +1,31 @@
using Diary.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// Add services to the container. //GetConfigStuff
IConfigurationRoot? config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json",false)
.Build();
builder.Services
.AddDbContext<DiaryDBContext>(opt =>
{
string connectionString = config.GetConnectionString("Diary");
opt.UseSqlServer(connectionString);
});
// Add services to the container.
builder.Services.AddDbContext<DiaryDBContext>();
builder.Services.AddControllers(); builder.Services.AddControllers();
builder.Services.AddScoped<IDiaryDBContext, DiaryDBContext>();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
@@ -12,8 +35,13 @@ var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
app.UseSwagger(); app.UseSwagger();
app.UseSwaggerUI(); app.UseSwaggerUI(opt =>
{
opt.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
opt.RoutePrefix = String.Empty;
}
);
} }
app.UseHttpsRedirection(); app.UseHttpsRedirection();
@@ -22,4 +50,4 @@ app.UseAuthorization();
app.MapControllers(); app.MapControllers();
app.Run(); app.Run();

View File

@@ -1,28 +1,13 @@
{ {
"$schema": "https://json.schemastore.org/launchsettings.json", "$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:10709",
"sslPort": 44301
}
},
"profiles": { "profiles": {
"Diary": { "Diary": {
"commandName": "Project", "commandName": "Project",
"dotnetRunMessages": true, "dotnetRunMessages": true,
"launchBrowser": true, "launchBrowser": true,
"launchUrl": "swagger", "launchUrl": "swagger",
"applicationUrl": "https://localhost:7252;http://localhost:5286", "applicationUrl": "https://localhost:5001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} }

View File

@@ -1,4 +1,10 @@
{ {
"Connectionstrings":
{
"Diary":"Server=192.168.1.20;Database=Dev_Diary;User Id=sa;Password=P@$$W0rd2021!.;"
},
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",