Skip to content
This repository was archived by the owner on Dec 11, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<byte[]>("RowVersion").IsRowVersion());
.Where(mt => typeof(Entity).GetTypeInfo().IsAssignableFrom(mt.GetClrType().GetTypeInfo()))
.ForAll(mt => modelBuilder.Entity(mt.GetClrType()).Property<byte[]>("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)
Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<Entity>()
.Single(ent => Equals(ent.Entity.GetType().GetTypeInfo(), navTargetTypeInfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ protected override IQueryable<TAggregateRoot> 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}]")
Expand Down