diff --git a/src/implementations/Backend.Fx.EfCorePersistence/DbContextExtensions.cs b/src/implementations/Backend.Fx.EfCorePersistence/DbContextExtensions.cs index 35668e25..8ca15084 100644 --- a/src/implementations/Backend.Fx.EfCorePersistence/DbContextExtensions.cs +++ b/src/implementations/Backend.Fx.EfCorePersistence/DbContextExtensions.cs @@ -1,12 +1,15 @@ using System; +using System.Diagnostics; using System.Globalization; using System.Linq; using System.Reflection; using Backend.Fx.BuildingBlocks; using Backend.Fx.Extensions; using Backend.Fx.Logging; +using JetBrains.Annotations; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; +using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.Extensions.Logging; using ILogger = Microsoft.Extensions.Logging.ILogger; @@ -27,16 +30,16 @@ public static void RegisterRowVersionProperty(this ModelBuilder modelBuilder) { modelBuilder.Model .GetEntityTypes() - .Where(mt => typeof(Entity).GetTypeInfo().IsAssignableFrom(mt.ClrType.GetTypeInfo())) - .ForAll(mt => modelBuilder.Entity(mt.ClrType).Property("RowVersion").IsRowVersion()); + .Where(mt => typeof(Entity).GetTypeInfo().IsAssignableFrom(mt.GetClrType().GetTypeInfo())) + .ForAll(mt => modelBuilder.Entity(mt.GetClrType()).Property("RowVersion").IsRowVersion()); } public static void RegisterEntityIdAsNeverGenerated(this ModelBuilder modelBuilder) { modelBuilder.Model .GetEntityTypes() - .Where(mt => typeof(Entity).GetTypeInfo().IsAssignableFrom(mt.ClrType.GetTypeInfo())) - .ForAll(mt => modelBuilder.Entity(mt.ClrType).Property(nameof(Entity.Id)).ValueGeneratedNever()); + .Where(mt => typeof(Entity).GetTypeInfo().IsAssignableFrom(mt.GetClrType().GetTypeInfo())) + .ForAll(mt => modelBuilder.Entity(mt.GetClrType()).Property(nameof(Entity.Id)).ValueGeneratedNever()); } public static void ApplyAggregateMappings(this DbContext dbContext, ModelBuilder modelBuilder) @@ -83,5 +86,18 @@ private static string GetPrimaryKeyValue(EntityEntry entry) { return (entry.Entity as Entity)?.Id.ToString(CultureInfo.InvariantCulture) ?? "?"; } + + internal static Type GetClrType([NotNull] this IMutableEntityType met) + { + if (met == null) throw new ArgumentNullException(nameof(met)); + var propertyInfo = met.GetType().GetProperty("ClrType"); + Debug.Assert(propertyInfo != null); + return (Type) propertyInfo.GetValue(met); + } + + internal static Type GetClrType([NotNull] this IEntityType et) + { + return et.ClrType; + } } } \ No newline at end of file diff --git a/src/implementations/Backend.Fx.EfCorePersistence/EfFlush.cs b/src/implementations/Backend.Fx.EfCorePersistence/EfFlush.cs index c9b25368..7679531b 100644 --- a/src/implementations/Backend.Fx.EfCorePersistence/EfFlush.cs +++ b/src/implementations/Backend.Fx.EfCorePersistence/EfFlush.cs @@ -155,7 +155,6 @@ private static EntityEntry GetAggregateRootEntry(ChangeTracker changeTracker, En Logger.LogDebug("Searching aggregate root of {EntityTypeName}[{Id}]", entry.Entity.GetType().Name, (entry.Entity as Identified)?.Id); foreach (NavigationEntry navigation in entry.Navigations) { - TypeInfo navTargetTypeInfo = navigation.Metadata.TargetEntityType.ClrType.GetTypeInfo(); int navigationTargetForeignKeyValue; if (navigation.CurrentValue == null) @@ -175,6 +174,7 @@ private static EntityEntry GetAggregateRootEntry(ChangeTracker changeTracker, En // assumption: an entity cannot be loaded on its own. Everything on the navigation path starting from the // aggregate root must have been loaded before, therefore we can find it using the change tracker + TypeInfo navTargetTypeInfo = navigation.Metadata.TargetEntityType.GetClrType().GetTypeInfo(); var navigationTargetEntry = changeTracker .Entries() .Single(ent => Equals(ent.Entity.GetType().GetTypeInfo(), navTargetTypeInfo) diff --git a/src/implementations/Backend.Fx.EfCorePersistence/EfRepository.cs b/src/implementations/Backend.Fx.EfCorePersistence/EfRepository.cs index 03258217..7c82d6e1 100644 --- a/src/implementations/Backend.Fx.EfCorePersistence/EfRepository.cs +++ b/src/implementations/Backend.Fx.EfCorePersistence/EfRepository.cs @@ -116,7 +116,7 @@ protected override IQueryable RawAggregateQueryable [SuppressMessage("ReSharper", "EF1001")] private void AuthorizeChanges(InternalEntityEntry entry, EntityState previousState) { - if (previousState == EntityState.Unchanged && entry.EntityState == EntityState.Modified && entry.EntityType.ClrType == typeof(TAggregateRoot)) + if (previousState == EntityState.Unchanged && entry.EntityState == EntityState.Modified && entry.EntityType.GetClrType() == typeof(TAggregateRoot)) { var aggregateRoot = (TAggregateRoot) entry.Entity; if (!_aggregateAuthorization.CanModify(aggregateRoot)) throw new ForbiddenException("Unauthorized attempt to modify {AggregateTypeName}[{aggregateRoot.Id}]")