我的 EF 模型定义如下:
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; }
}
我有以下测试代码:
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());
}
当我运行它时,测试失败,因为抛出了 ObjectDisposeException
并附带消息:
Safe handle has been closed
如果我将最后一行更改为:
}, options => options.Excluding(p => p.Role).Excluding(p => p.Permission));
然后测试运行成功。
仅有的两个嵌套对象是Role
和Permission
。当我明确排除它们时,测试有效,当我告诉它排除所有嵌套对象时,它似乎仍在尝试浏览它们。
有人遇到过这个吗?有什么解释可以解释为什么我认为应该发生的事情没有发生吗?
最佳答案
您正在使用ExclusionNestedObjects
,这意味着它不会在Role
和Permission
对象公开的对象之间进行结构比较。这些是根对象的属性。但它仍然会尝试进行简单的相等检查。
关于c# - 设置 ExclusionNestedObjects 时,FluentAssertions 导致 ObjectDisposeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52522480/