Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Audit.EntityFramework.Core: Fix bug when auditing tables with composi…
…te multiple froeign keys related to the same column (#178)
- Loading branch information
1 parent
3677cac
commit 97b2830
Showing
32 changed files
with
235 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
CREATE DATABASE Demo | ||
GO | ||
USE [Demo] | ||
GO | ||
|
||
CREATE TABLE [dbo].[Tenant]( | ||
[Id] [int] NOT NULL, | ||
[Name] varchar(100) NOT NULL, | ||
CONSTRAINT [PK_Tenant] PRIMARY KEY CLUSTERED | ||
( | ||
[Id] ASC | ||
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] | ||
) ON [PRIMARY] | ||
GO | ||
|
||
CREATE TABLE [dbo].[Employee]( | ||
[Id] [int] NOT NULL, | ||
[TenantId] [int] NOT NULL, | ||
[Name] varchar(100) NOT NULL, | ||
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED | ||
( | ||
[Id] ASC, | ||
[TenantId] ASC | ||
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] | ||
) ON [PRIMARY] | ||
GO | ||
|
||
ALTER TABLE [dbo].[Employee] WITH CHECK ADD CONSTRAINT [FK_Employee_Tenant] FOREIGN KEY([TenantId]) | ||
REFERENCES [dbo].[Tenant] ([Id]) | ||
GO | ||
|
||
ALTER TABLE [dbo].[Employee] CHECK CONSTRAINT [FK_Employee_Tenant] | ||
GO | ||
|
||
CREATE TABLE [dbo].[PettyCashTransaction]( | ||
[Id] [int] NOT NULL, | ||
[EmployeeId] [int] NULL, | ||
[TrusteeId] [int] NULL, | ||
[TenantId] [int] NULL, | ||
CONSTRAINT [PK_PettyCashTransaction] PRIMARY KEY CLUSTERED | ||
( | ||
[Id] ASC | ||
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] | ||
) ON [PRIMARY] | ||
GO | ||
|
||
ALTER TABLE [dbo].[PettyCashTransaction] WITH CHECK ADD CONSTRAINT [FK_PettyCashTransaction_Employee] FOREIGN KEY([EmployeeId], [TenantId]) | ||
REFERENCES [dbo].[Employee] ([Id], [TenantId]) | ||
GO | ||
|
||
ALTER TABLE [dbo].[PettyCashTransaction] CHECK CONSTRAINT [FK_PettyCashTransaction_Employee] | ||
GO | ||
|
||
ALTER TABLE [dbo].[PettyCashTransaction] WITH CHECK ADD CONSTRAINT [FK_PettyCashTransaction_EmployeeTrustee] FOREIGN KEY([TrusteeId], [TenantId]) | ||
REFERENCES [dbo].[Employee] ([Id], [TenantId]) | ||
GO | ||
|
||
ALTER TABLE [dbo].[PettyCashTransaction] CHECK CONSTRAINT [FK_PettyCashTransaction_EmployeeTrustee] | ||
GO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Text; | ||
|
||
namespace Audit.EntityFramework.Core.UnitTest | ||
{ | ||
public class DemoContext : AuditDbContext | ||
{ | ||
public const string CnnString = "data source=localhost;initial catalog=Demo;integrated security=true;"; | ||
|
||
public DbSet<Tenant> Tenants { get; set; } | ||
public DbSet<Employee> Employees { get; set; } | ||
public DbSet<PettyCashTransaction> Pettys { get; set; } | ||
|
||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
{ | ||
optionsBuilder.UseSqlServer(CnnString); | ||
optionsBuilder.EnableSensitiveDataLogging(); | ||
} | ||
|
||
protected override void OnModelCreating(ModelBuilder _) | ||
{ | ||
_.Entity<Tenant>().ToTable("Tenant"); | ||
_.Entity<Employee>().ToTable("Employee"); | ||
_.Entity<PettyCashTransaction>().ToTable("PettyCashTransaction"); | ||
|
||
_.Entity<Tenant>() | ||
.HasKey(t => t.Id); | ||
|
||
_.Entity<Employee>() | ||
.HasKey(t => new { t.Id, t.TenantId }); | ||
|
||
_.Entity<PettyCashTransaction>() | ||
.HasKey(t => new { t.Id }); | ||
|
||
_.Entity<PettyCashTransaction>() | ||
.HasOne(t => t.Employee) | ||
.WithMany() | ||
.HasForeignKey(t => new { t.EmployeeId, t.TenantId }); | ||
|
||
_.Entity<PettyCashTransaction>() | ||
.HasOne(t => t.Trustee) | ||
.WithMany() | ||
.HasForeignKey(t => new { t.TrusteeId, t.TenantId }); | ||
} | ||
} | ||
|
||
public class Tenant | ||
{ | ||
public int Id { get; set; } | ||
public string Name { get; set; } | ||
} | ||
public class Employee | ||
{ | ||
public int Id { get; set; } | ||
public int TenantId { get; set; } | ||
public string Name { get; set; } | ||
} | ||
public class PettyCashTransaction | ||
{ | ||
public int Id { get; set; } | ||
public int EmployeeId { get; set; } | ||
public int TrusteeId { get; set; } | ||
public int TenantId { get; set; } | ||
|
||
public Employee Employee { get; set; } | ||
public Employee Trustee { get; set; } | ||
} | ||
} |
Oops, something went wrong.