FLuent nHibernate Linq 查询
我在使用 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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
FluentNHibernate 中三元关联的映射取决于您的场景和域模型的具体情况。
基本上,如果您要在任何地方都有多对多关系(即您的模块可以有多个权限,角色可以有多个权限,角色可以有多个模块),您需要该关系有一个单独的实体,称为RoleModulePermission。它将把你的图形从三角形(3个类)变成星形(4个类具有共同的“根”)。新实体将具有三个多对一关系,并且
角色
、模块
和权限
将具有 >一对多与RoleModulePermission
的关系。设置级联,您可以尝试像查询其他模型一样查询此模型:如果您的模型受到更多限制,即您有一些约束,您可以尝试使用
AsTernaryAssociation
或AsEntityMap< 来更简单地映射它/code>,这几乎是一样的。然后在您的
Role
、Module
和Permission
类中,您应该具有类型IDictionary
的关系。这可能意味着权限 X 将角色 Y 映射到模块 Z,但这意味着在权限 X 内角色 Y 没有其他模块。 POCO 类中类似的东西:和映射中类似的东西:
确实支持它,但是。您必须在关系的每一侧指定一对多或多对多关系(在 POCO 的
角色
中,< code>Modules 和Permission
)并像这样映射每个关系:使用 LINQ 进行查询应该看起来像这样:
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 andRole
,Module
andPermission
will have one-to-many relation toRoleModulePermission
. Set up the cascades and you can try querying this model like any other:If your model is more restricted, i.e. you have some constraints, you can try to map it simpler using
AsTernaryAssociation
orAsEntityMap
, which is pretty much the same. Then in yourRole
,Module
andPermission
classes you should have relation of typeIDictionary<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:and like this in mapping:
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
andPermission
) and map each relation like that:Querying using LINQ should then look somehow like this: