• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lucaslorentz / auto-compute / 11986839382

23 Nov 2024 11:38AM UTC coverage: 80.885% (-0.05%) from 80.939%
11986839382

push

github

lucaslorentz
Add support for self referencing computeds

53 of 61 new or added lines in 9 files covered. (86.89%)

38 existing lines in 3 files now uncovered.

1481 of 1831 relevant lines covered (80.88%)

117.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/LLL.AutoCompute.EFCore/Metadata/Internal/ComputedNavigation.cs
1
using System.Linq.Expressions;
2
using LLL.AutoCompute.EFCore.Internal;
3
using Microsoft.EntityFrameworkCore;
4
using Microsoft.EntityFrameworkCore.ChangeTracking;
5
using Microsoft.EntityFrameworkCore.Metadata;
6

7
namespace LLL.AutoCompute.EFCore.Metadata.Internal;
8

9
public abstract class ComputedNavigation : ComputedMember
10
{
11
}
12

13
public class ComputedNavigation<TEntity, TProperty>(
×
14
    INavigationBase navigation,
×
15
    IUnboundChangesProvider<IEFCoreComputedInput, TEntity, TProperty> changesProvider
×
16
) : ComputedNavigation
×
17
    where TEntity : class
18
{
19
    public override IUnboundChangesProvider<IEFCoreComputedInput, TEntity, TProperty> ChangesProvider => changesProvider;
×
NEW
20
    public override INavigationBase Property => navigation;
×
21

22
    public override string ToDebugString()
23
    {
NEW
24
        return navigation.ToString()!;
×
25
    }
26

27
    public override async Task<IReadOnlySet<object>> Update(DbContext dbContext)
28
    {
NEW
29
        var updatedEntities = new HashSet<object>();
×
30
        var input = dbContext.GetComputedInput();
×
31
        var changes = await changesProvider.GetChangesAsync(input, null);
×
32
        foreach (var (entity, change) in changes)
×
33
        {
34
            var entityEntry = dbContext.Entry(entity);
×
NEW
35
            var navigationEntry = entityEntry.Navigation(navigation);
×
36

37
            var newValue = ChangesProvider.ChangeCalculation.IsIncremental
×
38
                ? ChangesProvider.ChangeCalculation.ApplyChange(
×
39
                    GetOriginalValue(navigationEntry),
×
40
                    change)
×
41
                : change;
×
42

43
            if (!navigationEntry.IsLoaded && entityEntry.State != EntityState.Detached)
×
44
                await navigationEntry.LoadAsync();
×
45

46
            if (!Equals(navigationEntry.CurrentValue, newValue))
×
47
            {
48
                navigationEntry.CurrentValue = newValue;
×
NEW
49
                updatedEntities.Add(entity);
×
50
            }
51
        }
NEW
52
        return updatedEntities;
×
53
    }
54

55
    public override async Task Fix(object ent, DbContext dbContext)
56
    {
57
        await FixInconsistency((TEntity)ent, dbContext);
×
58
    }
59

60
    public async Task FixInconsistency(TEntity entity, DbContext dbContext)
61
    {
62
        var entityEntry = dbContext.Entry(entity);
×
NEW
63
        var navigationEntry = entityEntry.Navigation(navigation);
×
64

65
        var newValue = ((Expression<Func<TEntity, TProperty>>)ChangesProvider.Expression).Compile()(entity);
×
66

67
        if (!Equals(navigationEntry.CurrentValue, newValue))
×
68
        {
69
            navigationEntry.CurrentValue = newValue;
×
70
        }
71
    }
72

73
    private static TProperty GetOriginalValue(NavigationEntry navigationEntry)
74
    {
75
        if (navigationEntry.EntityEntry.State == EntityState.Added)
×
76
            return default!;
×
77

78
        return (TProperty)navigationEntry.GetOriginalValue()!;
×
79
    }
80
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc