Skip to content

Commit

Permalink
v5.9.0 has been released.
Browse files Browse the repository at this point in the history
  • Loading branch information
TanvirArjel committed Jun 9, 2022
1 parent 2a4f66c commit cf73e0b
Show file tree
Hide file tree
Showing 21 changed files with 337 additions and 138 deletions.
37 changes: 35 additions & 2 deletions demo/AspNetCore5.0/Controllers/EmployeeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@ public class EmployeeController : Controller
{
private readonly IRepository _repository;
private readonly IQueryRepository _queryRepository;
private readonly DemoDbContext _context;
private readonly Demo1DbContext _context;
private readonly IRepository<Demo1DbContext> _demo1Repository;

public EmployeeController(
IRepository repository,
IQueryRepository queryRepository,
DemoDbContext context)
Demo1DbContext context,
IRepository<Demo1DbContext> demo1Repository)
{
_repository = repository;
_context = context;
_queryRepository = queryRepository;
_demo1Repository = demo1Repository;
}

// GET: Employee
Expand Down Expand Up @@ -76,6 +79,8 @@ public async Task<IActionResult> Create(Employee employee)
{
if (ModelState.IsValid)
{

// Insert to database 1
IDbContextTransaction transaction = await _repository.BeginTransactionAsync(IsolationLevel.ReadCommitted);

try
Expand All @@ -102,6 +107,34 @@ public async Task<IActionResult> Create(Employee employee)
await transaction.RollbackAsync();
}

// Insert to database 2
IDbContextTransaction transaction2 = await _demo1Repository.BeginTransactionAsync(IsolationLevel.ReadCommitted);

try
{
employee.EmployeeId = 0;
employee.DepartmentId = 1;

object[] primaryKeys = await _demo1Repository.InsertAsync(employee);


long employeeId = (long)primaryKeys[0];
EmployeeHistory employeeHistory = new EmployeeHistory()
{
EmployeeId = employeeId,
DepartmentId = employee.DepartmentId,
EmployeeName = employee.EmployeeName
};

await _demo1Repository.InsertAsync(employeeHistory);

await transaction2.CommitAsync();
}
catch (Exception)
{
await transaction2.RollbackAsync();
}

return RedirectToAction(nameof(Index));

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace AspNetCore5._0.Data
{
public class DemoDbContext : DbContext
public class Demo1DbContext : DbContext
{
public DemoDbContext(DbContextOptions<DemoDbContext> options) : base(options)
public Demo1DbContext(DbContextOptions<Demo1DbContext> options) : base(options)
{

}
Expand Down
28 changes: 28 additions & 0 deletions demo/AspNetCore5.0/Data/Demo2DbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using AspNetCore5._0.Data.Models;
using Microsoft.EntityFrameworkCore;

namespace AspNetCore5._0.Data
{
public class Demo2DbContext : DbContext
{
public Demo2DbContext(DbContextOptions<Demo2DbContext> options) : base(options)
{

}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
if (modelBuilder == null)
{
throw new ArgumentNullException(nameof(modelBuilder));
}

base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Employee>().HasKey(e => e.EmployeeId);
}

public DbSet<Employee> Employee { get; set; }
public DbSet<EmployeeHistory> EmployeeHistories { get; set; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.EntityFrameworkCore.Migrations;

namespace AspNetCore5._0.Migrations
namespace AspNetCore5._0.Data.Migrations1
{
public partial class InitialCreate : Migration
{
Expand All @@ -19,6 +19,19 @@ protected override void Up(MigrationBuilder migrationBuilder)
table.PrimaryKey("PK_Department", x => x.Id);
});

migrationBuilder.CreateTable(
name: "EmployeeHistories",
columns: table => new
{
EmployeeId = table.Column<long>(type: "bigint", nullable: false),
DepartmentId = table.Column<int>(type: "int", nullable: false),
EmployeeName = table.Column<string>(type: "nvarchar(max)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_EmployeeHistories", x => x.EmployeeId);
});

migrationBuilder.CreateTable(
name: "Employee",
columns: table => new
Expand Down Expand Up @@ -51,6 +64,9 @@ protected override void Down(MigrationBuilder migrationBuilder)
migrationBuilder.DropTable(
name: "Employee");

migrationBuilder.DropTable(
name: "EmployeeHistories");

migrationBuilder.DropTable(
name: "Department");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

namespace AspNetCore5._0.Migrations
namespace AspNetCore5._0.Data.Migrations1
{
[DbContext(typeof(DemoDbContext))]
partial class DemoDbContextModelSnapshot : ModelSnapshot
[DbContext(typeof(Demo1DbContext))]
partial class Demo1DbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.UseIdentityColumns()
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("ProductVersion", "5.0.2");
.HasAnnotation("ProductVersion", "5.0.17")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

modelBuilder.Entity("AspNetCore5._0.Data.Models.Department", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int")
.UseIdentityColumn();
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

b.Property<string>("Name")
.HasColumnType("nvarchar(max)");
Expand All @@ -38,7 +38,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<long>("EmployeeId")
.ValueGeneratedOnAdd()
.HasColumnType("bigint")
.UseIdentityColumn();
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

b.Property<int>("DepartmentId")
.HasColumnType("int");
Expand Down Expand Up @@ -78,13 +78,18 @@ protected override void BuildModel(ModelBuilder modelBuilder)
modelBuilder.Entity("AspNetCore5._0.Data.Models.Employee", b =>
{
b.HasOne("AspNetCore5._0.Data.Models.Department", "Department")
.WithMany()
.WithMany("Employees")
.HasForeignKey("DepartmentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();

b.Navigation("Department");
});

modelBuilder.Entity("AspNetCore5._0.Data.Models.Department", b =>
{
b.Navigation("Employees");
});
#pragma warning restore 612, 618
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cf73e0b

Please sign in to comment.