Skip to content

Commit

Permalink
Audit.EntityFramework.Core: Fix bug when auditing tables with composi…
Browse files Browse the repository at this point in the history
…te multiple froeign keys related to the same column (#178)
  • Loading branch information
thepirat000 committed Nov 15, 2018
1 parent 3677cac commit 97b2830
Show file tree
Hide file tree
Showing 32 changed files with 235 additions and 118 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,10 @@ All notable changes to Audit.NET and its extensions will be documented in this f

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

## [13.2.2] - 2018-11-15
### Modified
- Audit.EntityFramework.Core: Fix bug when auditing tables with composite multiple froeign keys related to the same column (#178).

## [13.2.1] - 2018-11-12
### Modified
- Audit.EntityFramework and Audit.EntityFramework.Core: Fix parameter default for `IgnoreMatchedProperties(bool)` on configuration API to be _true_.
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.DynamicProxy/Audit.DynamicProxy.csproj
Expand Up @@ -4,7 +4,7 @@
<Description>Generate Audit Logs by intercepting operation calls on any class without changing its code.</Description>
<Copyright>Copyright 2016</Copyright>
<AssemblyTitle>Audit.DynamicProxy</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFrameworks>netstandard1.5;net45</TargetFrameworks>
<DefineConstants>$(DefineConstants);STRONG_NAME</DefineConstants>
Expand Down
Expand Up @@ -4,7 +4,7 @@
<Description>Generate Audit Logs from EntityFramework context changes</Description>
<Copyright>Copyright 2016</Copyright>
<AssemblyTitle>Audit.EntityFramework.Core</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFrameworks>netstandard1.5;netstandard2.0;net461</TargetFrameworks>
<DefineConstants>$(DefineConstants);STRONG_NAME</DefineConstants>
Expand Down
Expand Up @@ -4,7 +4,7 @@
<Description>Generate Audit Logs from EntityFramework identity context changes</Description>
<Copyright>Copyright 2018</Copyright>
<AssemblyTitle>Audit.EntityFramework.Identity.Core</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFrameworks>netstandard1.5;netstandard2.0;net461</TargetFrameworks>
<DefineConstants>$(DefineConstants);STRONG_NAME</DefineConstants>
Expand Down
Expand Up @@ -4,7 +4,7 @@
<Description>Generate Audit Logs from EntityFramework identity context changes</Description>
<Copyright>Copyright 2018</Copyright>
<AssemblyTitle>Audit.EntityFramework.Identity</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFrameworks>netstandard1.5;netstandard2.0;net45</TargetFrameworks>
<DefineConstants>$(DefineConstants);STRONG_NAME</DefineConstants>
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.EntityFramework/Audit.EntityFramework.csproj
Expand Up @@ -4,7 +4,7 @@
<Description>Generate Audit Logs from EntityFramework context changes</Description>
<Copyright>Copyright 2016</Copyright>
<AssemblyTitle>Audit.EntityFramework</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFrameworks>netstandard1.5;netstandard2.0;net45</TargetFrameworks>
<DefineConstants>$(DefineConstants);STRONG_NAME</DefineConstants>
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.EntityFramework/DbContextHelper.Core.cs
Expand Up @@ -175,7 +175,7 @@ private static IEntityType GetDefiningType(DbContext dbContext, EntityEntry entr
{
foreach (var prop in fk.Properties)
{
result.Add(GetColumnName(prop), entry.Property(prop.Name).CurrentValue);
result[GetColumnName(prop)] = entry.Property(prop.Name).CurrentValue;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.FileSystem/Audit.FileSystem.csproj
Expand Up @@ -4,7 +4,7 @@
<Description>Generate Audit Logs from file system events using a FileSystemWatcher</Description>
<Copyright>Copyright 2018</Copyright>
<AssemblyTitle>Audit.FileSystem</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFrameworks>netstandard1.3;net45</TargetFrameworks>
<DefineConstants>$(DefineConstants);STRONG_NAME</DefineConstants>
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.Mvc.Core/Audit.Mvc.Core.csproj
Expand Up @@ -4,7 +4,7 @@
<Description>Generate detailed Audit Logs for AspNet Core MVC Controller actions.</Description>
<Copyright>Copyright 2016</Copyright>
<AssemblyTitle>Audit.Mvc.Core</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFrameworks>netstandard1.6;netstandard2.0;net451</TargetFrameworks>
<DefineConstants>TRACE;DEBUG;STRONG_NAME</DefineConstants>
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.Mvc/Audit.Mvc.csproj
Expand Up @@ -4,7 +4,7 @@
<Description>Generate detailed Audit Logs for MVC Controller actions.</Description>
<Copyright>Copyright 2016</Copyright>
<AssemblyTitle>Audit.Mvc</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFrameworks>netstandard1.6;netstandard2.0;net45</TargetFrameworks>
<DefineConstants>TRACE;DEBUG;STRONG_NAME</DefineConstants>
Expand Down
Expand Up @@ -4,7 +4,7 @@
<Description>Store Audit.NET Trail Logs into an Azure Document DB database</Description>
<Copyright>Copyright 2016</Copyright>
<AssemblyTitle>Audit.AzureDocumentDB</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFrameworks>net45;netstandard1.6;netstandard2.0</TargetFrameworks>
<DefineConstants>$(DefineConstants);STRONG_NAME</DefineConstants>
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.NET.AzureStorage/Audit.NET.AzureStorage.csproj
Expand Up @@ -4,7 +4,7 @@
<Description>Store Audit.NET Logs into an Azure Blob Storage</Description>
<Copyright>Copyright 2016</Copyright>
<AssemblyTitle>Audit.AzureStorage</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFrameworks>netstandard1.3;net45</TargetFrameworks>
<DefineConstants>$(DefineConstants);STRONG_NAME</DefineConstants>
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.NET.DynamoDB/Audit.NET.DynamoDB.csproj
Expand Up @@ -4,7 +4,7 @@
<Description>Store Audit.NET Trail Logs into Amazon DynamoDB</Description>
<Copyright>Copyright 2018</Copyright>
<AssemblyTitle>Audit.DynamoDB</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFrameworks>netstandard1.3;net45</TargetFrameworks>
<DefineConstants>$(DefineConstants);STRONG_NAME</DefineConstants>
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.NET.ElasticSearch/Audit.NET.Elasticsearch.csproj
Expand Up @@ -4,7 +4,7 @@
<Description>Store Audit.NET Trail Logs into Elasticsearch database</Description>
<Copyright>Copyright 2018</Copyright>
<AssemblyTitle>Audit.Elasticsearch</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFrameworks>net451;netstandard1.3</TargetFrameworks>
<DefineConstants>$(DefineConstants);STRONG_NAME</DefineConstants>
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.NET.EventLog.Core/Audit.NET.EventLog.Core.csproj
Expand Up @@ -4,7 +4,7 @@
<Description>Log Audit.NET events to the Windows EventLog when targeting net standard 2.0</Description>
<Copyright>Copyright 2018</Copyright>
<AssemblyTitle>Audit.NET.EventLog.Core</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFramework>netstandard2.0</TargetFramework>
<DefineConstants>$(DefineConstants);STRONG_NAME</DefineConstants>
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.NET.MongoDB/Audit.NET.MongoDB.csproj
Expand Up @@ -4,7 +4,7 @@
<Description>Store Audit.NET Trail Logs into a MongoDB database</Description>
<Copyright>Copyright 2016</Copyright>
<AssemblyTitle>Audit.MongoDB</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFrameworks>netstandard1.5;net45</TargetFrameworks>
<NoWarn>$(NoWarn);1591</NoWarn>
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.NET.MySql/Audit.NET.MySql.csproj
Expand Up @@ -4,7 +4,7 @@
<Description>Store Audit.NET Trail Logs into a MySQL database</Description>
<Copyright>Copyright 2016</Copyright>
<AssemblyTitle>Audit.MySql</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFrameworks>netcoreapp1.0;net451</TargetFrameworks>
<DefineConstants>$(DefineConstants);STRONG_NAME</DefineConstants>
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.NET.PostgreSql/Audit.NET.PostgreSql.csproj
Expand Up @@ -3,7 +3,7 @@
<Description>Store Audit.NET Trail Logs into a PostgreSQL database</Description>
<Copyright>Copyright 2017</Copyright>
<AssemblyTitle>Audit.PostgreSql</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFrameworks>netstandard2.0;netstandard1.3;net45</TargetFrameworks>
<DefineConstants>$(DefineConstants);STRONG_NAME</DefineConstants>
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.NET.Redis/Audit.NET.Redis.csproj
Expand Up @@ -4,7 +4,7 @@
<Description>Store Audit.NET Trail Logs in a Redis database as a strings, lists, hashes, sortedsets or pubsub</Description>
<Copyright>Copyright 2017</Copyright>
<AssemblyTitle>Audit.Redis</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFrameworks>netstandard1.5;net45</TargetFrameworks>
<DefineConstants>$(DefineConstants);STRONG_NAME</DefineConstants>
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.NET.SqlServer/Audit.NET.SqlServer.csproj
Expand Up @@ -4,7 +4,7 @@
<Description>Store Audit.NET Trail Logs into a SQL Server database</Description>
<Copyright>Copyright 2016</Copyright>
<AssemblyTitle>Audit.SqlServer</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFrameworks>netstandard2.0;netstandard1.3;net45</TargetFrameworks>
<DefineConstants>$(DefineConstants);STRONG_NAME</DefineConstants>
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.NET.Udp/Audit.NET.Udp.csproj
Expand Up @@ -4,7 +4,7 @@
<Description>Send Audit.NET Trail Logs as connectionless UDP datagrams to a remote host or a multicast group</Description>
<Copyright>Copyright 2017</Copyright>
<AssemblyTitle>Audit.Udp</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFrameworks>netstandard1.3;net45</TargetFrameworks>
<DefineConstants>$(DefineConstants);STRONG_NAME</DefineConstants>
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.NET.log4net/Audit.NET.log4net.csproj
Expand Up @@ -4,7 +4,7 @@
<Description>Log Audit.NET Audit Trail using Apache log4net™</Description>
<Copyright>Copyright 2017</Copyright>
<AssemblyTitle>Audit.log4net</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFrameworks>netstandard1.3;net45</TargetFrameworks>
<DefineConstants>$(DefineConstants);STRONG_NAME</DefineConstants>
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.NET/Audit.NET.csproj
Expand Up @@ -3,7 +3,7 @@
<Description>An extensible framework to audit executing operations in .NET and .NET Core.</Description>
<Copyright>Copyright 2016</Copyright>
<AssemblyTitle>Audit.NET</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFrameworks>netstandard1.3;netstandard2.0;net45</TargetFrameworks>
<DefineConstants>$(DefineConstants);STRONG_NAME</DefineConstants>
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.SignalR/Audit.SignalR.csproj
Expand Up @@ -3,7 +3,7 @@
<Description>Generate Audit Logs for ASP.NET SignalR invokations</Description>
<Copyright>Copyright 2016</Copyright>
<AssemblyTitle>Audit.SignalR</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFramework>net45</TargetFramework>
<DefineConstants>$(DefineConstants);STRONG_NAME</DefineConstants>
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.WCF/Audit.WCF.csproj
Expand Up @@ -4,7 +4,7 @@
<Description>Generate detailed Audit Logs for WCF service calls.</Description>
<Copyright>Copyright 2016</Copyright>
<AssemblyTitle>Audit.Wcf</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFramework>net45</TargetFramework>
<DefineConstants>$(DefineConstants);STRONG_NAME</DefineConstants>
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.WebApi.Core/Audit.WebApi.Core.csproj
Expand Up @@ -4,7 +4,7 @@
<Description>Generate detailed Audit Logs for AspNet Core Web API Controller calls.</Description>
<Copyright>Copyright 2016</Copyright>
<AssemblyTitle>Audit.WebApi.Core</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFrameworks>netstandard1.6;netstandard2.0;net451</TargetFrameworks>
<DefineConstants>TRACE;DEBUG;STRONG_NAME</DefineConstants>
Expand Down
2 changes: 1 addition & 1 deletion src/Audit.WebApi/Audit.WebApi.csproj
Expand Up @@ -4,7 +4,7 @@
<Description>Generate detailed Audit Logs for Web API Controller calls.</Description>
<Copyright>Copyright 2016</Copyright>
<AssemblyTitle>Audit.WebApi</AssemblyTitle>
<VersionPrefix>13.2.1</VersionPrefix>
<VersionPrefix>13.2.2</VersionPrefix>
<Authors>Federico Colombo</Authors>
<TargetFrameworks>netstandard1.6;netstandard2.0;net45</TargetFrameworks>
<DefineConstants>TRACE;DEBUG;STRONG_NAME</DefineConstants>
Expand Down
59 changes: 59 additions & 0 deletions test/Audit.EntityFramework.Core.UnitTest/Demo.sql
@@ -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
71 changes: 71 additions & 0 deletions test/Audit.EntityFramework.Core.UnitTest/DemoContext.cs
@@ -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; }
}
}

0 comments on commit 97b2830

Please sign in to comment.