FLuent nHibernate Linq 查询

发布于 2024-11-26 22:05:03 字数 2156 浏览 1 评论 0原文

我在使用 nHibernate 和 LINQ 访问桥接表以获取值时遇到问题。 我有 4 个表: ROLES 、 MODULES 、 PERMISSIONS 和 RoleModulePermission(bridge)。

角色包含 ROLEID(pk) 、 ROLENAME
模块包含 MODULEID(pk) , MODULENAME
权限包含 PERMISSIONID , PERMISSIONTYPE
RoleModulePermission 包含 ROLEID 、 MODULEID 和 PERMISSIONID

我想根据ROLEID获取应用于ROLES的模块n权限。

在此处输入图像描述

角色映射

  Table("tblRoles");
        Id(role => role.RoleID).GeneratedBy.Identity();
        Map(role => role.RoleName).Not.Nullable();
        Map(role => role.IsActive).Not.Nullable();
        Map(role => role.Description).Not.Nullable();

        HasManyToMany(x => x.Users)
            .Table("tblUserInRoles")
            .ParentKeyColumn("RoleID")
            .ChildKeyColumn("UserID")
            .Not.LazyLoad();

        HasManyToMany(x => x.Modules)
            .Table("tblRolesPermission")
            .ParentKeyColumn("RoleID")
            .ChildKeyColumn("ModuleID")
            .Not.LazyLoad();

        HasManyToMany(x => x.Permissions)
            .Table("tblRolesPermission")
            .ParentKeyColumn("RoleID")
            .ChildKeyColumn("PermissionID")
            .Not.LazyLoad();

模块映射

 Table("tblAppModules");
        Id(mod => mod.ModuleID).GeneratedBy.Identity();
        Map(mod => mod.ModuleName).Nullable();
        Map(mod => mod.CreationDate).Nullable();

        HasManyToMany(x => x.Roles)
            .Table("tblRolesPermission")
            .ParentKeyColumn("ModuleID")
            .ChildKeyColumn("RoleID")
            .Not.LazyLoad();

权限映射

Table("tblPermission");
        Id(p => p.PermissionID).GeneratedBy.Identity();
        Map(p => p.PermissionType).Not.Nullable();

        HasManyToMany(p => p.PermitRole)
            .Table("tblRolesPermission")
            .ParentKeyColumn("PermissionID")
            .ChildKeyColumn("RoleID")
            .Not.LazyLoad();

看来我在映射时做错了?

请不要在 tblRolesPermission 中假设“AllowAccess” 如何实现这一目标?

谢谢

I m facing issue to access the bridge table to get the values using nHibernate and LINQ.
I have 4 table, ROLES , MODULES , PERMISSIONS and the RoleModulePermission(bridge).

Roles contains ROLEID(pk) , ROLENAME

Modules contains MODULEID(pk) , MODULENAME

Permission contains PERMISSIONID , PERMISSIONTYPE

RoleModulePermission contains ROLEID , MODULEID and PERMISSIONID

I want to get the Modules n Permission applied to the ROLES on the basis of ROLEID.

enter image description here

Role Mapping

  Table("tblRoles");
        Id(role => role.RoleID).GeneratedBy.Identity();
        Map(role => role.RoleName).Not.Nullable();
        Map(role => role.IsActive).Not.Nullable();
        Map(role => role.Description).Not.Nullable();

        HasManyToMany(x => x.Users)
            .Table("tblUserInRoles")
            .ParentKeyColumn("RoleID")
            .ChildKeyColumn("UserID")
            .Not.LazyLoad();

        HasManyToMany(x => x.Modules)
            .Table("tblRolesPermission")
            .ParentKeyColumn("RoleID")
            .ChildKeyColumn("ModuleID")
            .Not.LazyLoad();

        HasManyToMany(x => x.Permissions)
            .Table("tblRolesPermission")
            .ParentKeyColumn("RoleID")
            .ChildKeyColumn("PermissionID")
            .Not.LazyLoad();

Module Mapping

 Table("tblAppModules");
        Id(mod => mod.ModuleID).GeneratedBy.Identity();
        Map(mod => mod.ModuleName).Nullable();
        Map(mod => mod.CreationDate).Nullable();

        HasManyToMany(x => x.Roles)
            .Table("tblRolesPermission")
            .ParentKeyColumn("ModuleID")
            .ChildKeyColumn("RoleID")
            .Not.LazyLoad();

Permission Mapping

Table("tblPermission");
        Id(p => p.PermissionID).GeneratedBy.Identity();
        Map(p => p.PermissionType).Not.Nullable();

        HasManyToMany(p => p.PermitRole)
            .Table("tblRolesPermission")
            .ParentKeyColumn("PermissionID")
            .ChildKeyColumn("RoleID")
            .Not.LazyLoad();

It seems that i did wrong in mapping ?

please do not assume 'AllowAccess' in tblRolesPermission
How to achieve this ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

埖埖迣鎅 2024-12-03 22:05:03

FluentNHibernate 中三元关联的映射取决于您的场景和域模型的具体情况。

基本上,如果您要在任何地方都有多对多关系(即您的模块可以有多个权限,角色可以有多个权限,角色可以有多个模块),您需要该关系有一个单独的实体,称为RoleModulePermission。它将把你的图形从三角形(3个类)变成星形(4个类具有共同的“根”)。新实体将具有三个多对一关系,并且角色模块权限将具有 >一对多RoleModulePermission的关系。设置级联,您可以尝试像查询其他模型一样查询此模型:

session.Query<Permission>()
    .Where(x => x.RoleModules.Any(rm => rm.Module == module));

如果您的模型受到更多限制,即您有一些约束,您可以尝试使用 AsTernaryAssociationAsEntityMap< 来更简单地映射它/code>,这几乎是一样的。然后在您的 RoleModulePermission 类中,您应该具有类型 IDictionary 的关系。这可能意味着权限 X 将角色 Y 映射到模块 Z,但这意味着在权限 X 内角色 Y 没有其他模块。 POCO 类中类似的东西:

 public virtual IDictionary<Role, Module> RoleModules { get; set; }

和映射中类似的东西:

HasManyToMany(x => x.RoleModules).Table("tblRolesPermissions").AsEntityMap();

确实支持它,但是。您必须在关系的每一侧指定一对多多对多关系(在 POCO 的角色中,< code>Modules 和 Permission)并像这样映射每个关系:

m.HasMany(x => x.RolesPermissionsModules).AsTernaryAssociation()

使用 LINQ 进行查询应该看起来像这样:

session.Query<Permission>()
    .Where(x => x.RoleModules[role] == module);

Mapping for ternary associations in FluentNHibernate depends from specifics of your scenario and domain model.

Basically, if you are going to have the many-to-many relations everywhere (i.e. your Module can have multiple Permissions, Role can have multiple Permissions and Role can have multiple Modules), you'll need to have a separate entity for the relation, called i.e. RoleModulePermission. It will change your graph from triangle-like (3 classes) into a star-like (4 classes with common "root"). The new entity will have three many-to-one relations and Role, Module and Permission will have one-to-many relation to RoleModulePermission. Set up the cascades and you can try querying this model like any other:

session.Query<Permission>()
    .Where(x => x.RoleModules.Any(rm => rm.Module == module));

If your model is more restricted, i.e. you have some constraints, you can try to map it simpler using AsTernaryAssociation or AsEntityMap, which is pretty much the same. Then in your Role, Module and Permission classes you should have relation of type IDictionary<TFrom, To>. This can mean i.e. that Permission X maps Role Y to Module Z, but it means that within Permission X Role Y have no other Modules. Something like this in POCO class:

 public virtual IDictionary<Role, Module> RoleModules { get; set; }

and like this in mapping:

HasManyToMany(x => x.RoleModules).Table("tblRolesPermissions").AsEntityMap();

does support it, but. You have to specify one-to-many or many-to-many relations at each side of the relation (in POCO's for Roles, Modules and Permission) and map each relation like that:

m.HasMany(x => x.RolesPermissionsModules).AsTernaryAssociation()

Querying using LINQ should then look somehow like this:

session.Query<Permission>()
    .Where(x => x.RoleModules[role] == module);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文