other way to log

main
syneffort 2 years ago
parent 0c431ce57f
commit cbce3bf7ed
  1. 64
      MySolution/EF_ChangeLog/Controller/StockDbContext.cs
  2. 22
      MySolution/EF_ChangeLog/EF_ChangeLog.csproj
  3. 77
      MySolution/EF_ChangeLog/Migrations/20231018044706_initialize.Designer.cs
  4. 55
      MySolution/EF_ChangeLog/Migrations/20231018044706_initialize.cs
  5. 81
      MySolution/EF_ChangeLog/Migrations/20231018051741_log_reinforce.Designer.cs
  6. 29
      MySolution/EF_ChangeLog/Migrations/20231018051741_log_reinforce.cs
  7. 85
      MySolution/EF_ChangeLog/Migrations/20231018052343_log_reinforce2.Designer.cs
  8. 29
      MySolution/EF_ChangeLog/Migrations/20231018052343_log_reinforce2.cs
  9. 85
      MySolution/EF_ChangeLog/Migrations/20231018052414_log_reinforce3.Designer.cs
  10. 28
      MySolution/EF_ChangeLog/Migrations/20231018052414_log_reinforce3.cs
  11. 85
      MySolution/EF_ChangeLog/Migrations/20231018052426_log_reinforce4.Designer.cs
  12. 22
      MySolution/EF_ChangeLog/Migrations/20231018052426_log_reinforce4.cs
  13. 111
      MySolution/EF_ChangeLog/Migrations/20231018061903_log_reinforce5.Designer.cs
  14. 38
      MySolution/EF_ChangeLog/Migrations/20231018061903_log_reinforce5.cs
  15. 108
      MySolution/EF_ChangeLog/Migrations/StockDbContextModelSnapshot.cs
  16. 26
      MySolution/EF_ChangeLog/Model/Stock.cs
  17. 21
      MySolution/EF_ChangeLog/Model/StockChangeLog.cs
  18. 17
      MySolution/EF_ChangeLog/Model/StockLog.cs
  19. 51
      MySolution/EF_ChangeLog/Program.cs
  20. 10
      MySolution/MySolution.sln

@ -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<Stock> Stocks { get; set; }
public DbSet<StockChangeLog> StockChangeLogs { get; set; }
public DbSet<StockLog> 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();
}
}
}

@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.12">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.12" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.12">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>

@ -0,0 +1,77 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<decimal>("Price")
.HasColumnType("decimal(18,2)");
b.HasKey("Id");
b.ToTable("Stocks");
});
modelBuilder.Entity("EF_ChangeLog.Model.StockChangeLog", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ChangeBy")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("ChangeDate")
.HasColumnType("datetime2");
b.Property<int>("EntityId")
.HasColumnType("int");
b.Property<string>("EntityName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("StockChangeLogs");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,55 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace EF_ChangeLog.Migrations
{
/// <inheritdoc />
public partial class initialize : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "StockChangeLogs",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
EntityName = table.Column<string>(type: "nvarchar(max)", nullable: false),
EntityId = table.Column<int>(type: "int", nullable: false),
ChangeBy = table.Column<string>(type: "nvarchar(max)", nullable: false),
ChangeDate = table.Column<DateTime>(type: "datetime2", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_StockChangeLogs", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Stocks",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<decimal>(type: "decimal(18,2)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Stocks", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "StockChangeLogs");
migrationBuilder.DropTable(
name: "Stocks");
}
}
}

@ -0,0 +1,81 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<decimal>("Price")
.HasColumnType("decimal(18,2)");
b.HasKey("Id");
b.ToTable("Stocks");
});
modelBuilder.Entity("EF_ChangeLog.Model.StockChangeLog", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ChangeBy")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("ChangeDate")
.HasColumnType("datetime2");
b.Property<int>("EntityId")
.HasColumnType("int");
b.Property<string>("EntityName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("OriginalSnapshot")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("StockChangeLogs");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace EF_ChangeLog.Migrations
{
/// <inheritdoc />
public partial class log_reinforce : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "OriginalSnapshot",
table: "StockChangeLogs",
type: "nvarchar(max)",
nullable: false,
defaultValue: "");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "OriginalSnapshot",
table: "StockChangeLogs");
}
}
}

@ -0,0 +1,85 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<decimal>("Price")
.HasColumnType("decimal(18,2)");
b.HasKey("Id");
b.ToTable("Stocks");
});
modelBuilder.Entity("EF_ChangeLog.Model.StockChangeLog", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Action")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("ChangeBy")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("ChangeDate")
.HasColumnType("datetime2");
b.Property<int>("EntityId")
.HasColumnType("int");
b.Property<string>("EntityName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("OriginalSnapshot")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("StockChangeLogs");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace EF_ChangeLog.Migrations
{
/// <inheritdoc />
public partial class log_reinforce2 : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "Action",
table: "StockChangeLogs",
type: "nvarchar(max)",
nullable: false,
defaultValue: "");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Action",
table: "StockChangeLogs");
}
}
}

@ -0,0 +1,85 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<decimal>("Price")
.HasColumnType("decimal(18,2)");
b.HasKey("Id");
b.ToTable("Stocks");
});
modelBuilder.Entity("EF_ChangeLog.Model.StockChangeLog", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ActionType")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("ChangeBy")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("ChangeDate")
.HasColumnType("datetime2");
b.Property<int>("EntityId")
.HasColumnType("int");
b.Property<string>("EntityName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("OriginalSnapshot")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("StockChangeLogs");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace EF_ChangeLog.Migrations
{
/// <inheritdoc />
public partial class log_reinforce3 : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.RenameColumn(
name: "Action",
table: "StockChangeLogs",
newName: "ActionType");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.RenameColumn(
name: "ActionType",
table: "StockChangeLogs",
newName: "Action");
}
}
}

@ -0,0 +1,85 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<decimal>("Price")
.HasColumnType("decimal(18,2)");
b.HasKey("Id");
b.ToTable("Stocks");
});
modelBuilder.Entity("EF_ChangeLog.Model.StockChangeLog", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ActionType")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("ChangeBy")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("ChangeDate")
.HasColumnType("datetime2");
b.Property<int>("EntityId")
.HasColumnType("int");
b.Property<string>("EntityName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("OriginalSnapshot")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("StockChangeLogs");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,22 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace EF_ChangeLog.Migrations
{
/// <inheritdoc />
public partial class log_reinforce4 : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}

@ -0,0 +1,111 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<decimal>("Price")
.HasColumnType("decimal(18,2)");
b.HasKey("Id");
b.ToTable("Stocks");
});
modelBuilder.Entity("EF_ChangeLog.Model.StockChangeLog", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ActionType")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("ChangeBy")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("ChangeDate")
.HasColumnType("datetime2");
b.Property<int>("EntityId")
.HasColumnType("int");
b.Property<string>("EntityName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("OriginalSnapshot")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("StockChangeLogs");
});
modelBuilder.Entity("EF_ChangeLog.Model.StockLog", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<DateTime>("LoggedAt")
.HasColumnType("datetime2");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("OriginalId")
.HasColumnType("int");
b.Property<decimal>("Price")
.HasColumnType("decimal(18,2)");
b.HasKey("Id");
b.ToTable("stockLogs");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,38 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace EF_ChangeLog.Migrations
{
/// <inheritdoc />
public partial class log_reinforce5 : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "stockLogs",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
OriginalId = table.Column<int>(type: "int", nullable: false),
Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
LoggedAt = table.Column<DateTime>(type: "datetime2", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_stockLogs", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "stockLogs");
}
}
}

@ -0,0 +1,108 @@
// <auto-generated />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<decimal>("Price")
.HasColumnType("decimal(18,2)");
b.HasKey("Id");
b.ToTable("Stocks");
});
modelBuilder.Entity("EF_ChangeLog.Model.StockChangeLog", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ActionType")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("ChangeBy")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("ChangeDate")
.HasColumnType("datetime2");
b.Property<int>("EntityId")
.HasColumnType("int");
b.Property<string>("EntityName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("OriginalSnapshot")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("StockChangeLogs");
});
modelBuilder.Entity("EF_ChangeLog.Model.StockLog", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<DateTime>("LoggedAt")
.HasColumnType("datetime2");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("OriginalId")
.HasColumnType("int");
b.Property<decimal>("Price")
.HasColumnType("decimal(18,2)");
b.HasKey("Id");
b.ToTable("stockLogs");
});
#pragma warning restore 612, 618
}
}
}

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

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

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

@ -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<Stock> newStocks = new List<Stock>()
{
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();
}
}
}
}
}

@ -15,9 +15,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TAPClient", "TAPClient\TAPC
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SObject", "SObject\SObject.csproj", "{98E66698-89A2-49DF-A808-279C3FEFFABF}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SObject", "SObject\SObject.csproj", "{98E66698-89A2-49DF-A808-279C3FEFFABF}"
EndProject 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 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 EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.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.ActiveCfg = Release|Any CPU
{CDF5F490-9F8C-4CB2-AA5D-2460CBD43CC1}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

Loading…
Cancel
Save