Skip to content

Commit

Permalink
Fixing issue with GetColumnName when a property is mapped to JSON usi…
Browse files Browse the repository at this point in the history
…ng EF Core 7 (#555)
  • Loading branch information
thepirat000 committed Dec 14, 2022
1 parent bc52765 commit 1567f7f
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ 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/).

## [20.1.2] - 2022-12-14
- Audit.EntityFramework.Core: Fixing issue with GetColumnName when a property is mapped to JSON using EF Core 7 (#555)

## [20.1.1] - 2022-12-12
- Audit.Mvc: Adding `IncludeChildActions` configuration to the `AuditAttribute` action filter for MVC 5 (#554)

Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>20.1.1</Version>
<Version>20.1.2</Version>
<PackageReleaseNotes>
</PackageReleaseNotes>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<DefineConstants>$(DefineConstants);EF_CORE_6;EF_CORE_2_OR_GREATER;EF_CORE_3_OR_GREATER;EF_CORE_5_OR_GREATER;EF_CORE_6_OR_GREATER</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'net7.0' ">
<DefineConstants>$(DefineConstants);EF_CORE_7;EF_CORE_2_OR_GREATER;EF_CORE_3_OR_GREATER;EF_CORE_5_OR_GREATER;EF_CORE_6_OR_GREATER</DefineConstants>
<DefineConstants>$(DefineConstants);EF_CORE_7;EF_CORE_2_OR_GREATER;EF_CORE_3_OR_GREATER;EF_CORE_5_OR_GREATER;EF_CORE_6_OR_GREATER;EF_CORE_7_OR_GREATER</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net7.0' ">
<DefineConstants>$(DefineConstants);EF_CORE_7;EF_CORE_2_OR_GREATER;EF_CORE_3_OR_GREATER;EF_CORE_5_OR_GREATER;EF_CORE_6_OR_GREATER</DefineConstants>
<DefineConstants>$(DefineConstants);EF_CORE_7;EF_CORE_2_OR_GREATER;EF_CORE_3_OR_GREATER;EF_CORE_5_OR_GREATER;EF_CORE_6_OR_GREATER;EF_CORE_7_OR_GREATER</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down
7 changes: 6 additions & 1 deletion src/Audit.EntityFramework/DbContextHelper.Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ private List<EventEntryChange> GetChanges(IAuditDbContext context, EntityEntry e
/// </summary>
private static string GetColumnName(IProperty prop)
{
#if EF_CORE_5_OR_GREATER
#if EF_CORE_7_OR_GREATER
var storeObjectIdentifier = StoreObjectIdentifier.Create(prop.DeclaringEntityType, StoreObjectType.Table);
return storeObjectIdentifier.HasValue
? (prop.GetColumnName(storeObjectIdentifier.Value) ?? prop.GetDefaultColumnName())
: prop.GetDefaultColumnName();
#elif EF_CORE_5_OR_GREATER
var storeObjectIdentifier = StoreObjectIdentifier.Create(prop.DeclaringEntityType, StoreObjectType.Table);
return storeObjectIdentifier.HasValue
? prop.GetColumnName(storeObjectIdentifier.Value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net7.0' ">
<DefineConstants>$(DefineConstants);EF_CORE_7;EF_CORE_2_OR_GREATER;EF_CORE_3_OR_GREATER;EF_CORE_5_OR_GREATER;EF_CORE_6_OR_GREATER</DefineConstants>
<DefineConstants>$(DefineConstants);EF_CORE_7;EF_CORE_2_OR_GREATER;EF_CORE_3_OR_GREATER;EF_CORE_5_OR_GREATER;EF_CORE_6_OR_GREATER;EF_CORE_7_OR_GREATER</DefineConstants>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.0' ">
Expand Down
34 changes: 34 additions & 0 deletions test/Audit.EntityFramework.Core.UnitTest/Contexts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,40 @@

namespace Audit.EntityFramework.Core.UnitTest
{
#if EF_CORE_7_OR_GREATER
[AuditDbContext(IncludeEntityObjects = true)]
public class Context_OwnedEntity_ToJson : AuditDbContext
{
public class Person
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string City { get; set; }
public string Street { get; set; }
}

public DbSet<Person> People { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(@$"Server=(localdb)\mssqllocaldb;Database={nameof(Context_OwnedEntity_ToJson)};Trusted_Connection=True;ConnectRetryCount=0").UseLazyLoadingProxies();
}
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Person>().OwnsOne(e => e.Address, b => b.ToJson());
}
}
#endif

#if EF_CORE_5_OR_GREATER
[AuditDbContext(IncludeEntityObjects = true)]
public class Context_OwnedEntity : AuditDbContext
Expand Down
82 changes: 82 additions & 0 deletions test/Audit.EntityFramework.Core.UnitTest/EfCoreInMemoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,88 @@ public void Setup()
Audit.Core.Configuration.ResetCustomActions();
}

#if EF_CORE_7_OR_GREATER
[Test]
public void Test_EF_OwnedEntity_ToJson()
{
using var context = new Context_OwnedEntity_ToJson();
var evs = new List<EntityFrameworkEvent>();
Audit.Core.Configuration.Setup()
.UseDynamicProvider(_ => _.OnInsertAndReplace(ev =>
{
evs.Add(ev.GetEntityFrameworkEvent());
}));

context.Database.EnsureDeleted();
context.Database.EnsureCreated();

context.People.Add(new Context_OwnedEntity_ToJson.Person()
{
Id = 1,
Name = "Development",
Address = new Context_OwnedEntity_ToJson.Address { City = "Vienna", Street = "Street" },
});

context.SaveChanges();

Assert.AreEqual(1, evs.Count);

Assert.AreEqual(2, evs[0].Entries.Count);

Assert.AreEqual("Insert", evs[0].Entries[0].Action);
Assert.AreEqual("Insert", evs[0].Entries[1].Action);

Assert.AreEqual(1, evs[0].Entries[0].ColumnValues["Id"]);
Assert.AreEqual("Development", evs[0].Entries[0].ColumnValues["Name"]);

Assert.AreEqual("Vienna", evs[0].Entries[1].ColumnValues["City"]);
Assert.AreEqual("Street", evs[0].Entries[1].ColumnValues["Street"]);

Assert.AreEqual(1, ((dynamic)evs[0].Entries[0].Entity).Id);
Assert.AreEqual("Vienna", ((dynamic)evs[0].Entries[0].Entity).Address.City);
}

[Test]
public async Task Test_EF_OwnedEntity_ToJson_Async()
{
using var context = new Context_OwnedEntity_ToJson();
var evs = new List<EntityFrameworkEvent>();
Audit.Core.Configuration.Setup()
.UseDynamicProvider(_ => _.OnInsertAndReplace(ev =>
{
evs.Add(ev.GetEntityFrameworkEvent());
}));

await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();

await context.People.AddAsync(new Context_OwnedEntity_ToJson.Person()
{
Id = 1,
Name = "Development",
Address = new Context_OwnedEntity_ToJson.Address { City = "Vienna", Street = "Street" },
});

await context.SaveChangesAsync();

Assert.AreEqual(1, evs.Count);

Assert.AreEqual(2, evs[0].Entries.Count);

Assert.AreEqual("Insert", evs[0].Entries[0].Action);
Assert.AreEqual("Insert", evs[0].Entries[1].Action);

Assert.AreEqual(1, evs[0].Entries[0].ColumnValues["Id"]);
Assert.AreEqual("Development", evs[0].Entries[0].ColumnValues["Name"]);

Assert.AreEqual("Vienna", evs[0].Entries[1].ColumnValues["City"]);
Assert.AreEqual("Street", evs[0].Entries[1].ColumnValues["Street"]);

Assert.AreEqual(1, ((dynamic)evs[0].Entries[0].Entity).Id);
Assert.AreEqual("Vienna", ((dynamic)evs[0].Entries[0].Entity).Address.City);
}
#endif

[Test]
public void Test_EF_SaveChangesGetAudit()
{
Expand Down

0 comments on commit 1567f7f

Please sign in to comment.