Skip to content

Commit

Permalink
Exclude proxies collection SN-776
Browse files Browse the repository at this point in the history
  • Loading branch information
vasiliy-chefonov committed Feb 6, 2025
1 parent 67a2a04 commit 4201c14
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
8 changes: 7 additions & 1 deletion src/Saritasa.NetForge/Domain/Extensions/CloneExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ public static class CloneExtensions

var serializeSettings = new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,

// Ignore errors during serialization (in case of shadow properties in LazyLoadingProxies).
Error = (serializer, err) =>
{
err.ErrorContext.Handled = true;
}
};

serializeSettings.Converters.Add(new DecimalJsonConverter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,14 @@ public async Task<object> UpdateAsync(
private async Task UpdateAsync(
DbContext dbContext, object entity, object originalEntity, CancellationToken cancellationToken)
{
dbContext.Attach(originalEntity);
try
{
dbContext.Attach(originalEntity);
}
catch (InvalidOperationException)
{
// Do nothing. It means that entity is already tracked.
}

await UpdateNavigations(dbContext, entity, originalEntity);

Expand Down Expand Up @@ -295,13 +302,15 @@ private static void UpdateNavigationReference(
}
else
{
var isTracked = dbContext.IsTracked(navigationEntry.CurrentValue!, comparer);

if (!isTracked)
try
{
dbContext.Attach(navigationEntry.CurrentValue!);
originalNavigationEntry.CurrentValue = navigationEntry.CurrentValue;
}
catch (InvalidOperationException)
{
// Do nothing. It means that entity is already tracked.
}
originalNavigationEntry.CurrentValue = navigationEntry.CurrentValue;
}
}

Expand All @@ -316,12 +325,14 @@ private static void UpdateNavigationCollection(
// Track added elements
foreach (var element in navigationCollectionInstance)
{
var isTracked = dbContext.IsTracked(element, comparer);

if (!isTracked)
try
{
dbContext.Attach(element);
}
catch (InvalidOperationException)
{
// Do nothing. It means that entity is already tracked.
}
}

var originalNavigationCollectionInstance = (IEnumerable<object>)originalNavigationEntry.CurrentValue!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static class ProxyToPocoConverter
foreach (var property in entityType.GetProperties())
{
// Exclude the navigation properties because they are the proxies as well.
if (navigationPropertyNames != null && !navigationPropertyNames.Contains(property.Name))
if (navigationPropertyNames != null && navigationPropertyNames.Contains(property.Name))
{
continue;
}
Expand All @@ -63,17 +63,27 @@ public static class ProxyToPocoConverter
{
var value = property.GetValue(source);

// Skip the property if it is a proxy.
if (value != null && value.GetType().IsLazyLoadingProxy())
{
continue;
}

// Check if the property is a collection of proxy entities.
if (value is IEnumerable<object> collection)
{
var firstItem = collection.FirstOrDefault();
if (firstItem != null && firstItem.GetType().IsLazyLoadingProxy())
{
continue;
}
}

property.SetValue(pocoInstance, value);
}
catch
{
// Skip the property if it cannot be copied.
property.SetValue(pocoInstance, null);
}
}

Expand Down

0 comments on commit 4201c14

Please sign in to comment.