2

I have an EF model defined as follows:

public class ProgramManagerRolePermission
{
    [Key, Column(Order = 1)]
    public Guid ShardKey { get; set; }

    [Key, Column(Order = 2)]
    public Guid RoleId { get; set; }

    [Key, Column(Order = 3)]
    public Guid PermissionId { get; set; }

    [ForeignKey(nameof(RoleId))]
    public virtual ProgramManagerRole Role { get; set; }

    [ForeignKey(nameof(PermissionId))]
    public virtual ProgramManagerPermission Permission { get; set; }
}

I've got the following test code:

using (var repo = IdentityProgramRepositoryFactory.Get())
{
    var role = repo.ProgramManagerRoles
        .Include(r => r.Permissions)
        .SingleOrDefault(r => r.Id == roleId);
    role.Permissions.Should().BeEquivalentTo(new[] {
        new Repo.ProgramManagerRolePermission
        {
            PermissionId = ProgramManagerPermissions.GrantOrRevokeRoles.Id,
            RoleId = roleId,
            ShardKey = identityProgramId
        },
        new Repo.ProgramManagerRolePermission
        {
            PermissionId = ProgramManagerPermissions.ManageNode.Id,
            RoleId = roleId,
            ShardKey = identityProgramId
        }
    }, options => options.ExcludingNestedObjects());
}

When I run it the test fails because an ObjectDisposedException is thrown with the message:

Safe handle has been closed

If I change the last line to:

}, options => options.Excluding(p => p.Role).Excluding(p => p.Permission));

Then the test runs successfully.

The only two nested objects are Role and Permission. When I exclude them explicitly the test works, when I tell it to exclude all nested objects it appears to still be trying to navigate through them.

Anyone run into this before? Any explanation as to why what I think should be happening isn't?

Craig W.
  • 17,838
  • 6
  • 49
  • 82

1 Answers1

2

You're using ExcludingNestedObjects, which means it will not do a structural comparison between the objects exposed by the Role and Permission objects. Those are properties of your root object. But it will still try to do a simple equality check.

Dennis Doomen
  • 8,368
  • 1
  • 32
  • 44
  • I misinterpreted what the intellisense was telling me for `ExcludingNestedObjects`. I incorrectly interpreted it as essentially meaning it would completely skip any property that was a reference type. Thanks for clearing it up. – Craig W. Sep 27 '18 at 16:05
  • Well, I can see that you could misinterpret this. – Dennis Doomen Sep 29 '18 at 07:26