From 633d64c4e0f7df8505f70fc301dc1488c37ee3f1 Mon Sep 17 00:00:00 2001 From: Matteo Locher Date: Fri, 7 Sep 2018 13:54:42 +0200 Subject: [PATCH] Fix filter to also take added entities into account --- .../Extensions/DbContextExtensions.cs | 4 ++-- .../AutoHistoryTest.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.EntityFrameworkCore.AutoHistory/Extensions/DbContextExtensions.cs b/src/Microsoft.EntityFrameworkCore.AutoHistory/Extensions/DbContextExtensions.cs index 3fdea44..d96ee46 100644 --- a/src/Microsoft.EntityFrameworkCore.AutoHistory/Extensions/DbContextExtensions.cs +++ b/src/Microsoft.EntityFrameworkCore.AutoHistory/Extensions/DbContextExtensions.cs @@ -29,8 +29,8 @@ public static void EnsureAutoHistory(this DbContext context, Func< where TAutoHistory : AutoHistory { // Must ToArray() here for excluding the AutoHistory model. - // Currently, only support Modified and Deleted entity. - var entries = context.ChangeTracker.Entries().Where(e => e.State == EntityState.Modified || e.State == EntityState.Deleted).ToArray(); + // Currently, only support Added, Modified and Deleted entity. + var entries = context.ChangeTracker.Entries().Where(e => e.State == EntityState.Modified || e.State == EntityState.Deleted || e.State == EntityState.Added).ToArray(); foreach (var entry in entries) { context.Add(entry.AutoHistory(createHistoryFactory)); diff --git a/test/Microsoft.EntityFrameworkCore.AutoHistory.Test/AutoHistoryTest.cs b/test/Microsoft.EntityFrameworkCore.AutoHistory.Test/AutoHistoryTest.cs index 47e1880..016641c 100644 --- a/test/Microsoft.EntityFrameworkCore.AutoHistory.Test/AutoHistoryTest.cs +++ b/test/Microsoft.EntityFrameworkCore.AutoHistory.Test/AutoHistoryTest.cs @@ -26,7 +26,7 @@ public void Entity_Add_AutoHistory_Test() var count = db.ChangeTracker.Entries().Count(e => e.State == EntityState.Added); - Assert.Equal(2, count); + Assert.Equal(4, count); // Blog, BlogPost, History for Blog, History for BlogPost } } [Fact]