From cbce3bf7ed3235c62cda09152262861e361028d6 Mon Sep 17 00:00:00 2001 From: syneffort Date: Wed, 18 Oct 2023 17:04:32 +0900 Subject: [PATCH] other way to log --- .../EF_ChangeLog/Controller/StockDbContext.cs | 64 ++++++++++ MySolution/EF_ChangeLog/EF_ChangeLog.csproj | 22 ++++ .../20231018044706_initialize.Designer.cs | 77 ++++++++++++ .../Migrations/20231018044706_initialize.cs | 55 +++++++++ .../20231018051741_log_reinforce.Designer.cs | 81 +++++++++++++ .../20231018051741_log_reinforce.cs | 29 +++++ .../20231018052343_log_reinforce2.Designer.cs | 85 ++++++++++++++ .../20231018052343_log_reinforce2.cs | 29 +++++ .../20231018052414_log_reinforce3.Designer.cs | 85 ++++++++++++++ .../20231018052414_log_reinforce3.cs | 28 +++++ .../20231018052426_log_reinforce4.Designer.cs | 85 ++++++++++++++ .../20231018052426_log_reinforce4.cs | 22 ++++ .../20231018061903_log_reinforce5.Designer.cs | 111 ++++++++++++++++++ .../20231018061903_log_reinforce5.cs | 38 ++++++ .../Migrations/StockDbContextModelSnapshot.cs | 108 +++++++++++++++++ MySolution/EF_ChangeLog/Model/Stock.cs | 26 ++++ .../EF_ChangeLog/Model/StockChangeLog.cs | 21 ++++ MySolution/EF_ChangeLog/Model/StockLog.cs | 17 +++ MySolution/EF_ChangeLog/Program.cs | 51 ++++++++ MySolution/MySolution.sln | 10 +- 20 files changed, 1042 insertions(+), 2 deletions(-) create mode 100644 MySolution/EF_ChangeLog/Controller/StockDbContext.cs create mode 100644 MySolution/EF_ChangeLog/EF_ChangeLog.csproj create mode 100644 MySolution/EF_ChangeLog/Migrations/20231018044706_initialize.Designer.cs create mode 100644 MySolution/EF_ChangeLog/Migrations/20231018044706_initialize.cs create mode 100644 MySolution/EF_ChangeLog/Migrations/20231018051741_log_reinforce.Designer.cs create mode 100644 MySolution/EF_ChangeLog/Migrations/20231018051741_log_reinforce.cs create mode 100644 MySolution/EF_ChangeLog/Migrations/20231018052343_log_reinforce2.Designer.cs create mode 100644 MySolution/EF_ChangeLog/Migrations/20231018052343_log_reinforce2.cs create mode 100644 MySolution/EF_ChangeLog/Migrations/20231018052414_log_reinforce3.Designer.cs create mode 100644 MySolution/EF_ChangeLog/Migrations/20231018052414_log_reinforce3.cs create mode 100644 MySolution/EF_ChangeLog/Migrations/20231018052426_log_reinforce4.Designer.cs create mode 100644 MySolution/EF_ChangeLog/Migrations/20231018052426_log_reinforce4.cs create mode 100644 MySolution/EF_ChangeLog/Migrations/20231018061903_log_reinforce5.Designer.cs create mode 100644 MySolution/EF_ChangeLog/Migrations/20231018061903_log_reinforce5.cs create mode 100644 MySolution/EF_ChangeLog/Migrations/StockDbContextModelSnapshot.cs create mode 100644 MySolution/EF_ChangeLog/Model/Stock.cs create mode 100644 MySolution/EF_ChangeLog/Model/StockChangeLog.cs create mode 100644 MySolution/EF_ChangeLog/Model/StockLog.cs create mode 100644 MySolution/EF_ChangeLog/Program.cs diff --git a/MySolution/EF_ChangeLog/Controller/StockDbContext.cs b/MySolution/EF_ChangeLog/Controller/StockDbContext.cs new file mode 100644 index 0000000..0c44b46 --- /dev/null +++ b/MySolution/EF_ChangeLog/Controller/StockDbContext.cs @@ -0,0 +1,64 @@ +using EF_ChangeLog.Model; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EF_ChangeLog.Controller +{ + internal class StockDbContext : DbContext + { + public DbSet Stocks { get; set; } + public DbSet StockChangeLogs { get; set; } + public DbSet stockLogs { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlServer(@"Data Source=peacecloud.synology.me,21433;Initial Catalog=Study;User ID=study;Password=Study1234;Encrypt=false"); + base.OnConfiguring(optionsBuilder); + } + + public override int SaveChanges() + { + var modifiedEntities = ChangeTracker.Entries().ToList(); + + for (var i = 0; i < modifiedEntities.Count; i++) + { + var entry = modifiedEntities[i]; + var entitiy = entry.Entity; + + if (entitiy.GetType() != typeof(Stock)) + continue; + + if (entitiy is Stock stock && entry.State != EntityState.Added) + { + StockLog stockLog = new StockLog() + { + OriginalId = (int)entry.OriginalValues[nameof(Stock.Id)], + Name = (string)entry.OriginalValues[nameof(Stock.Name)], + Price = (decimal)entry.OriginalValues[nameof(Stock.Price)], + LoggedAt = DateTime.Now + }; + + this.stockLogs.Add(stockLog); + } + + var changeLog = new StockChangeLog() + { + EntityName = entitiy.GetType().Name, + EntityId = (int)entitiy.GetType().GetProperty("Id").GetValue(entitiy), + ChangeBy = "User", + ChangeDate = DateTime.Now, + ActionType = entry.State.ToString(), + OriginalSnapshot = (string)entitiy.GetType().GetProperty("OriginalSnapshot").GetValue(entitiy) + }; + + StockChangeLogs.Add(changeLog); + } + + return base.SaveChanges(); + } + } +} diff --git a/MySolution/EF_ChangeLog/EF_ChangeLog.csproj b/MySolution/EF_ChangeLog/EF_ChangeLog.csproj new file mode 100644 index 0000000..c8919a0 --- /dev/null +++ b/MySolution/EF_ChangeLog/EF_ChangeLog.csproj @@ -0,0 +1,22 @@ + + + + Exe + net6.0 + enable + enable + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + diff --git a/MySolution/EF_ChangeLog/Migrations/20231018044706_initialize.Designer.cs b/MySolution/EF_ChangeLog/Migrations/20231018044706_initialize.Designer.cs new file mode 100644 index 0000000..efa9333 --- /dev/null +++ b/MySolution/EF_ChangeLog/Migrations/20231018044706_initialize.Designer.cs @@ -0,0 +1,77 @@ +// +using System; +using EF_ChangeLog.Controller; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace EF_ChangeLog.Migrations +{ + [DbContext(typeof(StockDbContext))] + [Migration("20231018044706_initialize")] + partial class initialize + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.12") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("EF_ChangeLog.Model.Stock", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("decimal(18,2)"); + + b.HasKey("Id"); + + b.ToTable("Stocks"); + }); + + modelBuilder.Entity("EF_ChangeLog.Model.StockChangeLog", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ChangeBy") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ChangeDate") + .HasColumnType("datetime2"); + + b.Property("EntityId") + .HasColumnType("int"); + + b.Property("EntityName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("StockChangeLogs"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MySolution/EF_ChangeLog/Migrations/20231018044706_initialize.cs b/MySolution/EF_ChangeLog/Migrations/20231018044706_initialize.cs new file mode 100644 index 0000000..9cbf6cc --- /dev/null +++ b/MySolution/EF_ChangeLog/Migrations/20231018044706_initialize.cs @@ -0,0 +1,55 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace EF_ChangeLog.Migrations +{ + /// + public partial class initialize : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "StockChangeLogs", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + EntityName = table.Column(type: "nvarchar(max)", nullable: false), + EntityId = table.Column(type: "int", nullable: false), + ChangeBy = table.Column(type: "nvarchar(max)", nullable: false), + ChangeDate = table.Column(type: "datetime2", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_StockChangeLogs", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Stocks", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "decimal(18,2)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Stocks", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "StockChangeLogs"); + + migrationBuilder.DropTable( + name: "Stocks"); + } + } +} diff --git a/MySolution/EF_ChangeLog/Migrations/20231018051741_log_reinforce.Designer.cs b/MySolution/EF_ChangeLog/Migrations/20231018051741_log_reinforce.Designer.cs new file mode 100644 index 0000000..408d8a1 --- /dev/null +++ b/MySolution/EF_ChangeLog/Migrations/20231018051741_log_reinforce.Designer.cs @@ -0,0 +1,81 @@ +// +using System; +using EF_ChangeLog.Controller; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace EF_ChangeLog.Migrations +{ + [DbContext(typeof(StockDbContext))] + [Migration("20231018051741_log_reinforce")] + partial class log_reinforce + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.12") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("EF_ChangeLog.Model.Stock", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("decimal(18,2)"); + + b.HasKey("Id"); + + b.ToTable("Stocks"); + }); + + modelBuilder.Entity("EF_ChangeLog.Model.StockChangeLog", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ChangeBy") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ChangeDate") + .HasColumnType("datetime2"); + + b.Property("EntityId") + .HasColumnType("int"); + + b.Property("EntityName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OriginalSnapshot") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("StockChangeLogs"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MySolution/EF_ChangeLog/Migrations/20231018051741_log_reinforce.cs b/MySolution/EF_ChangeLog/Migrations/20231018051741_log_reinforce.cs new file mode 100644 index 0000000..6614fe0 --- /dev/null +++ b/MySolution/EF_ChangeLog/Migrations/20231018051741_log_reinforce.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace EF_ChangeLog.Migrations +{ + /// + public partial class log_reinforce : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "OriginalSnapshot", + table: "StockChangeLogs", + type: "nvarchar(max)", + nullable: false, + defaultValue: ""); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "OriginalSnapshot", + table: "StockChangeLogs"); + } + } +} diff --git a/MySolution/EF_ChangeLog/Migrations/20231018052343_log_reinforce2.Designer.cs b/MySolution/EF_ChangeLog/Migrations/20231018052343_log_reinforce2.Designer.cs new file mode 100644 index 0000000..730a598 --- /dev/null +++ b/MySolution/EF_ChangeLog/Migrations/20231018052343_log_reinforce2.Designer.cs @@ -0,0 +1,85 @@ +// +using System; +using EF_ChangeLog.Controller; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace EF_ChangeLog.Migrations +{ + [DbContext(typeof(StockDbContext))] + [Migration("20231018052343_log_reinforce2")] + partial class log_reinforce2 + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.12") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("EF_ChangeLog.Model.Stock", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("decimal(18,2)"); + + b.HasKey("Id"); + + b.ToTable("Stocks"); + }); + + modelBuilder.Entity("EF_ChangeLog.Model.StockChangeLog", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Action") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ChangeBy") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ChangeDate") + .HasColumnType("datetime2"); + + b.Property("EntityId") + .HasColumnType("int"); + + b.Property("EntityName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OriginalSnapshot") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("StockChangeLogs"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MySolution/EF_ChangeLog/Migrations/20231018052343_log_reinforce2.cs b/MySolution/EF_ChangeLog/Migrations/20231018052343_log_reinforce2.cs new file mode 100644 index 0000000..65407f1 --- /dev/null +++ b/MySolution/EF_ChangeLog/Migrations/20231018052343_log_reinforce2.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace EF_ChangeLog.Migrations +{ + /// + public partial class log_reinforce2 : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "Action", + table: "StockChangeLogs", + type: "nvarchar(max)", + nullable: false, + defaultValue: ""); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "Action", + table: "StockChangeLogs"); + } + } +} diff --git a/MySolution/EF_ChangeLog/Migrations/20231018052414_log_reinforce3.Designer.cs b/MySolution/EF_ChangeLog/Migrations/20231018052414_log_reinforce3.Designer.cs new file mode 100644 index 0000000..652e30b --- /dev/null +++ b/MySolution/EF_ChangeLog/Migrations/20231018052414_log_reinforce3.Designer.cs @@ -0,0 +1,85 @@ +// +using System; +using EF_ChangeLog.Controller; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace EF_ChangeLog.Migrations +{ + [DbContext(typeof(StockDbContext))] + [Migration("20231018052414_log_reinforce3")] + partial class log_reinforce3 + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.12") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("EF_ChangeLog.Model.Stock", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("decimal(18,2)"); + + b.HasKey("Id"); + + b.ToTable("Stocks"); + }); + + modelBuilder.Entity("EF_ChangeLog.Model.StockChangeLog", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ActionType") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ChangeBy") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ChangeDate") + .HasColumnType("datetime2"); + + b.Property("EntityId") + .HasColumnType("int"); + + b.Property("EntityName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OriginalSnapshot") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("StockChangeLogs"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MySolution/EF_ChangeLog/Migrations/20231018052414_log_reinforce3.cs b/MySolution/EF_ChangeLog/Migrations/20231018052414_log_reinforce3.cs new file mode 100644 index 0000000..2b18a00 --- /dev/null +++ b/MySolution/EF_ChangeLog/Migrations/20231018052414_log_reinforce3.cs @@ -0,0 +1,28 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace EF_ChangeLog.Migrations +{ + /// + public partial class log_reinforce3 : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.RenameColumn( + name: "Action", + table: "StockChangeLogs", + newName: "ActionType"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.RenameColumn( + name: "ActionType", + table: "StockChangeLogs", + newName: "Action"); + } + } +} diff --git a/MySolution/EF_ChangeLog/Migrations/20231018052426_log_reinforce4.Designer.cs b/MySolution/EF_ChangeLog/Migrations/20231018052426_log_reinforce4.Designer.cs new file mode 100644 index 0000000..5102f4c --- /dev/null +++ b/MySolution/EF_ChangeLog/Migrations/20231018052426_log_reinforce4.Designer.cs @@ -0,0 +1,85 @@ +// +using System; +using EF_ChangeLog.Controller; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace EF_ChangeLog.Migrations +{ + [DbContext(typeof(StockDbContext))] + [Migration("20231018052426_log_reinforce4")] + partial class log_reinforce4 + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.12") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("EF_ChangeLog.Model.Stock", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("decimal(18,2)"); + + b.HasKey("Id"); + + b.ToTable("Stocks"); + }); + + modelBuilder.Entity("EF_ChangeLog.Model.StockChangeLog", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ActionType") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ChangeBy") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ChangeDate") + .HasColumnType("datetime2"); + + b.Property("EntityId") + .HasColumnType("int"); + + b.Property("EntityName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OriginalSnapshot") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("StockChangeLogs"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MySolution/EF_ChangeLog/Migrations/20231018052426_log_reinforce4.cs b/MySolution/EF_ChangeLog/Migrations/20231018052426_log_reinforce4.cs new file mode 100644 index 0000000..9941616 --- /dev/null +++ b/MySolution/EF_ChangeLog/Migrations/20231018052426_log_reinforce4.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace EF_ChangeLog.Migrations +{ + /// + public partial class log_reinforce4 : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + + } + } +} diff --git a/MySolution/EF_ChangeLog/Migrations/20231018061903_log_reinforce5.Designer.cs b/MySolution/EF_ChangeLog/Migrations/20231018061903_log_reinforce5.Designer.cs new file mode 100644 index 0000000..6d870e9 --- /dev/null +++ b/MySolution/EF_ChangeLog/Migrations/20231018061903_log_reinforce5.Designer.cs @@ -0,0 +1,111 @@ +// +using System; +using EF_ChangeLog.Controller; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace EF_ChangeLog.Migrations +{ + [DbContext(typeof(StockDbContext))] + [Migration("20231018061903_log_reinforce5")] + partial class log_reinforce5 + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.12") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("EF_ChangeLog.Model.Stock", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("decimal(18,2)"); + + b.HasKey("Id"); + + b.ToTable("Stocks"); + }); + + modelBuilder.Entity("EF_ChangeLog.Model.StockChangeLog", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ActionType") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ChangeBy") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ChangeDate") + .HasColumnType("datetime2"); + + b.Property("EntityId") + .HasColumnType("int"); + + b.Property("EntityName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OriginalSnapshot") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("StockChangeLogs"); + }); + + modelBuilder.Entity("EF_ChangeLog.Model.StockLog", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("LoggedAt") + .HasColumnType("datetime2"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OriginalId") + .HasColumnType("int"); + + b.Property("Price") + .HasColumnType("decimal(18,2)"); + + b.HasKey("Id"); + + b.ToTable("stockLogs"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MySolution/EF_ChangeLog/Migrations/20231018061903_log_reinforce5.cs b/MySolution/EF_ChangeLog/Migrations/20231018061903_log_reinforce5.cs new file mode 100644 index 0000000..86eb658 --- /dev/null +++ b/MySolution/EF_ChangeLog/Migrations/20231018061903_log_reinforce5.cs @@ -0,0 +1,38 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace EF_ChangeLog.Migrations +{ + /// + public partial class log_reinforce5 : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "stockLogs", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + OriginalId = table.Column(type: "int", nullable: false), + Name = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "decimal(18,2)", nullable: false), + LoggedAt = table.Column(type: "datetime2", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_stockLogs", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "stockLogs"); + } + } +} diff --git a/MySolution/EF_ChangeLog/Migrations/StockDbContextModelSnapshot.cs b/MySolution/EF_ChangeLog/Migrations/StockDbContextModelSnapshot.cs new file mode 100644 index 0000000..b3737f3 --- /dev/null +++ b/MySolution/EF_ChangeLog/Migrations/StockDbContextModelSnapshot.cs @@ -0,0 +1,108 @@ +// +using System; +using EF_ChangeLog.Controller; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace EF_ChangeLog.Migrations +{ + [DbContext(typeof(StockDbContext))] + partial class StockDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.12") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("EF_ChangeLog.Model.Stock", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("decimal(18,2)"); + + b.HasKey("Id"); + + b.ToTable("Stocks"); + }); + + modelBuilder.Entity("EF_ChangeLog.Model.StockChangeLog", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ActionType") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ChangeBy") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ChangeDate") + .HasColumnType("datetime2"); + + b.Property("EntityId") + .HasColumnType("int"); + + b.Property("EntityName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OriginalSnapshot") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("StockChangeLogs"); + }); + + modelBuilder.Entity("EF_ChangeLog.Model.StockLog", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("LoggedAt") + .HasColumnType("datetime2"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OriginalId") + .HasColumnType("int"); + + b.Property("Price") + .HasColumnType("decimal(18,2)"); + + b.HasKey("Id"); + + b.ToTable("stockLogs"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MySolution/EF_ChangeLog/Model/Stock.cs b/MySolution/EF_ChangeLog/Model/Stock.cs new file mode 100644 index 0000000..1ec6f17 --- /dev/null +++ b/MySolution/EF_ChangeLog/Model/Stock.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace EF_ChangeLog.Model +{ + internal class Stock + { + public int Id { get; set; } + public string Name { get; set; } + public decimal Price { get; set; } + + [NotMapped] + public string OriginalSnapshot { get; set; } + + public void SaveSnapshot() + { + this.OriginalSnapshot = JsonSerializer.Serialize(this); + } + } +} diff --git a/MySolution/EF_ChangeLog/Model/StockChangeLog.cs b/MySolution/EF_ChangeLog/Model/StockChangeLog.cs new file mode 100644 index 0000000..d58c615 --- /dev/null +++ b/MySolution/EF_ChangeLog/Model/StockChangeLog.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EF_ChangeLog.Model +{ + internal class StockChangeLog + { + [Key] + public int Id { get; set; } + public string EntityName { get; set; } + public int EntityId { get; set; } + public string ChangeBy { get; set; } + public DateTime ChangeDate { get; set; } + public string ActionType { get; set; } + public string OriginalSnapshot { get; set; } + } +} diff --git a/MySolution/EF_ChangeLog/Model/StockLog.cs b/MySolution/EF_ChangeLog/Model/StockLog.cs new file mode 100644 index 0000000..6f322b0 --- /dev/null +++ b/MySolution/EF_ChangeLog/Model/StockLog.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EF_ChangeLog.Model +{ + internal class StockLog + { + public int Id { get; set; } + public int OriginalId { get; set; } + public string Name { get; set; } + public decimal Price { get; set; } + public DateTime LoggedAt { get; set; } + } +} diff --git a/MySolution/EF_ChangeLog/Program.cs b/MySolution/EF_ChangeLog/Program.cs new file mode 100644 index 0000000..ed33eb3 --- /dev/null +++ b/MySolution/EF_ChangeLog/Program.cs @@ -0,0 +1,51 @@ +using EF_ChangeLog.Controller; +using EF_ChangeLog.Model; + +namespace EF_ChangeLog +{ + internal class Program + { + static void Main(string[] args) + { + using (var context = new StockDbContext()) + { + // Create samples + Random rand = new Random(); + List newStocks = new List() + { + new Stock() { Name = $"Test Stock{rand.Next(1, 1000)}", Price = 1000 }, + new Stock() { Name = $"Test Stock{rand.Next(1, 1000)}", Price = 1000 }, + new Stock() { Name = $"Test Stock{rand.Next(1, 1000)}", Price = 1000 }, + new Stock() { Name = $"Test Stock{rand.Next(1, 1000)}", Price = 1000 } + }; + + foreach (var newStock in newStocks) + { + newStock.SaveSnapshot(); + context.Stocks.Add(newStock); + } + context.SaveChanges(); + + // Update sample + var stock = context.Stocks.Find(rand.Next(1, 5)); + if (stock != null) + { + stock.Price = rand.Next(1, 10) * 1000; + + stock.SaveSnapshot(); + context.SaveChanges(); + } + + // Delete sample + var del = context.Stocks.OrderByDescending(s => s.Id).FirstOrDefault(); + if (del != null) + { + context.Stocks.Remove(del); + + del.SaveSnapshot(); + context.SaveChanges(); + } + } + } + } +} \ No newline at end of file diff --git a/MySolution/MySolution.sln b/MySolution/MySolution.sln index ac0102c..874fdff 100644 --- a/MySolution/MySolution.sln +++ b/MySolution/MySolution.sln @@ -15,9 +15,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TAPClient", "TAPClient\TAPC EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SObject", "SObject\SObject.csproj", "{98E66698-89A2-49DF-A808-279C3FEFFABF}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpStreamingApp", "HttpStreamingApp\HttpStreamingApp.csproj", "{3D549B90-D7ED-4771-8595-9147BBF221C6}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpStreamingApp", "HttpStreamingApp\HttpStreamingApp.csproj", "{3D549B90-D7ED-4771-8595-9147BBF221C6}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InventoryManagement", "InventoryManagement\InventoryManagement.csproj", "{CDF5F490-9F8C-4CB2-AA5D-2460CBD43CC1}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InventoryManagement", "InventoryManagement\InventoryManagement.csproj", "{CDF5F490-9F8C-4CB2-AA5D-2460CBD43CC1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EF_ChangeLog", "EF_ChangeLog\EF_ChangeLog.csproj", "{B3039951-7560-4D50-93AE-7E1B57AB8EA6}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -57,6 +59,10 @@ Global {CDF5F490-9F8C-4CB2-AA5D-2460CBD43CC1}.Debug|Any CPU.Build.0 = Debug|Any CPU {CDF5F490-9F8C-4CB2-AA5D-2460CBD43CC1}.Release|Any CPU.ActiveCfg = Release|Any CPU {CDF5F490-9F8C-4CB2-AA5D-2460CBD43CC1}.Release|Any CPU.Build.0 = Release|Any CPU + {B3039951-7560-4D50-93AE-7E1B57AB8EA6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B3039951-7560-4D50-93AE-7E1B57AB8EA6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B3039951-7560-4D50-93AE-7E1B57AB8EA6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B3039951-7560-4D50-93AE-7E1B57AB8EA6}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE