使用fluent api配置一对一关系时对象为null

我有三个类:Role,Permission和RolePermission(角色权限是第三个表在一个多对多的关系)


public class Role : Entity

{

    public string Name { get; set; }

    public string Description { get; set; }

    public virtual ICollection<RolePermission> RolePermissions { get; set; }

}


public class Permission : Entity

{

    public string Name { get; set; }

    public virtual ICollection<RolePermission> RolePermissions { get; set; }

}


public class RolePermission : Entity

{

    public int RoleId { get; set; }

    public int PermissionId { get; set; }

    public Permission Permission { get; set; }

    public Role Role { get; set; }

}

然后我用来fluentAPI配置关系:


对于Role:


HasMany(role => role.RolePermissions)

            .WithRequired(rolePermission => rolePermission.Role)

            .HasForeignKey(rolePermission => rolePermission.RoleId);

对于Permission:


HasMany(permission => permission.RolePermissions)

            .WithRequired(rolePermission => rolePermission.Permission)

            .HasForeignKey(rolePermission => rolePermission.PermissionId);

对于RolePermission:


HasRequired(rolePermission => rolePermission.Permission)

            .WithMany(permission => permission.RolePermissions)

            .HasForeignKey(rolePermission => rolePermission.PermissionId);


HasRequired(rolePermission => rolePermission.Role)

            .WithMany(role => role.RolePermissions)

            .HasForeignKey(rolePermission => rolePermission.RoleId);

问题是只Role填充了对象。

http://img.mukewang.com/61daacfa00013b3707680081.jpg

HUX布斯
浏览 201回答 2
2回答

慕虎7371278

此问题中的代码与建立关系有关。此问题中报告的问题与未自动加载的相关数据有关。这是两件不同的事情,彼此之间几乎没有关系。您是否在某处错过了包含?您是否访问过(并因此延迟加载) Role nav 属性,但没有访问 Permission nav 属性?我想查看从您启动查询的位置到您检查此对象的位置的代码(根据您的屏幕截图)您回复了请求的代码:var user = _userRepository&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .FirstOrDefaultAsync(us => us.Email == email);&nbsp;var userPermissions =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;user.UserRoles&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.First()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Role&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.RolePermissions&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Select(rp => rp.Permission)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.ToList();如果您Include()在查询中插入一条语句,您将看到Permission实际上将正确获取该语句。我不太确定您正在检查哪个对象。屏幕截图告诉我您正在查看 a RolePermission,但发布的代码建议您获取Permission对象列表。无论如何,您似乎已经使用以下方法修复了它Include:Mihai Alexandru-Ionut @Flater,是的,我必须使用include,问题就解决了。这是解决方案,因此请将其作为答案发布以接受它。

catspeake

IdRole和Permission表都缺少属性。当您说表中的RoleId属性时RolePermission,EF 会在表中查找 Id 属性Role。像这样更新您的角色和权限表并尝试一下:public class Role : Entity{&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; public string Name { get; set; }&nbsp; &nbsp; public string Description { get; set; }&nbsp; &nbsp; public virtual ICollection<RolePermission> RolePermissions { get; set; }}public class Permission : Entity{&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; public string Name { get; set; }&nbsp; &nbsp; public virtual ICollection<RolePermission> RolePermissions { get; set; }}
打开App,查看更多内容
随时随地看视频慕课网APP