NHibernate 映射代码 ManyToMany
NHibernate mapping by code ManyToMany
问题
我通过代码使用 NHibernate 映射来映射关系。在这种情况下,我将用户映射到角色再到权限。用户和角色是 n:m 关系,角色和权限也是如此。 SQL 数据库是 SQL 服务器。
如果我从角色和权限之间的代码中删除 n:m 关系车间,我的代码就可以工作。如果它在那里,我得到以下 MappingException
:
Could not determine type for:
Dtp.Entities.AppPrivilege, Dtp.Entities, for columns: NHibernate.Mapping.Column(id)
我找不到差异的根源,因为用户和角色之间的同一个关系可以正常工作。任何人都可以阐明这个问题吗?
产生错误的精确部分(如果注释掉它就会消失)是 AppRole.AppPrivileges
.
的 Bag
我的代码
应用用户
~~~Table~~~
AppUserId, uniqueidentifier, not null
//some omitted properties
~~~实体~~~
public class AppUser {
public virtual Guid AppUserId { get; set; }
//some omitted properties
public virtual IList<AppRole> AppRoles { get; set; }
}
~~~映射~~~
public class AppUserMap : ClassMapping<AppUser>
{
public AppUserMap()
{
Table("AppUser");
Schema("dbo");
Lazy(true);
Id(x => x.AppUserId, map => map.Generator(Generators.GuidComb));
//some omitted properties
Bag(x => x.AppRoles,
colmap => {
colmap.Cascade(Cascade.None);
colmap.Table("AppUser_AppRole");
colmap.Key(x => x.Column("AppUserId"));
},
map => map.ManyToMany(many => many.Column("AppRoleId")));
}
}
AppUser_AppRole
~~~Table~~~
AppUserId, uniqueidentifier, not null
AppRoleId, uniqueidentifier, not null
AppRole
~~~Table~~~
AppRoleId, uniqueidentifier, not null
//some omitted properties
~~~实体~~~
public class AppRole{
public virtual Guid AppRoleId { get; set; }
//some omitted properties
public virtual IList<AppUser> AppUsers { get; set; }
public virtual IList<AppPrivilege> AppPrivileges { get; set; }
}
~~~映射~~~
public class AppRoleMap : ClassMapping<AppRole> {
public AppRoleMap()
{
Table("AppRole");
Schema("dbo");
Lazy(true);
Id(x => x.AppRoleId, map => map.Generator(Generators.GuidComb));
//some omitted properties
Bag(x => x.AppUsers,
colmap => {
colmap.Cascade(Cascade.None);
colmap.Table("AppUser_AppRole");
colmap.Key(x => x.Column("AppRoleId"));
},
map => map.ManyToMany(many => many.Column("AppUserId")));
//The following definition produces the bug.
Bag(x => x.AppPrivileges,
colmap => {
colmap.Cascade(Cascade.None);
colmap.Table("AppRole_AppPrivilege");
colmap.Key(x => x.Column("AppRoleId"));
},
map => map.ManyToMany(many => many.Column("AppPrivilegeId")));
}
}
AppRole_AppPrivilege
~~~Table~~~
AppRoleId, uniqueidentifier, not null
AppPrivilegeId, uniqueidentifier, not null
AppPrivilege
~~~Table~~~
AppPrivilegeId, uniqueidentifier, not null
//some omitted properties
~~~实体~~~
public class AppPrivilege {
public virtual Guid AppPrivilegeId { get; set; }
//some omitted properties
public virtual IList<AppRole> AppRoles { get; set; }
}
~~~映射~~~
public class AppPrivilegeMap: ClassMapping<AppPrivilege> {
public AppPrivilegeMap()
{
Table("AppPrivilege");
Schema("dbo");
Lazy(true);
Id(x => x.AppPrivilegeId, map => map.Generator(Generators.GuidComb));
//some omitted properties
Bag(x => x.AppRoles,
colmap => {
colmap.Cascade(Cascade.None);
colmap.Table("AppRole_AppPrivilege");
colmap.Key(x => x.Column("AppPrivilegeId"));
},
map => map.ManyToMany(many => many.Column("AppRoleId")));
}
}
在上述文件中找不到此问题的答案。我只是忘记在 NHibernateHelper.OnConfigure()
期间将 AppPrivilegeMap
添加到 ModelMapper
。
现在最后的任务是找出.Inverse(true)
的放置位置,这样序列化一个对象就不会因为循环引用而导致异常了。
问题
我通过代码使用 NHibernate 映射来映射关系。在这种情况下,我将用户映射到角色再到权限。用户和角色是 n:m 关系,角色和权限也是如此。 SQL 数据库是 SQL 服务器。
如果我从角色和权限之间的代码中删除 n:m 关系车间,我的代码就可以工作。如果它在那里,我得到以下 MappingException
:
Could not determine type for:
Dtp.Entities.AppPrivilege, Dtp.Entities, for columns: NHibernate.Mapping.Column(id)
我找不到差异的根源,因为用户和角色之间的同一个关系可以正常工作。任何人都可以阐明这个问题吗?
产生错误的精确部分(如果注释掉它就会消失)是 AppRole.AppPrivileges
.
我的代码
应用用户
~~~Table~~~
AppUserId, uniqueidentifier, not null
//some omitted properties
~~~实体~~~
public class AppUser {
public virtual Guid AppUserId { get; set; }
//some omitted properties
public virtual IList<AppRole> AppRoles { get; set; }
}
~~~映射~~~
public class AppUserMap : ClassMapping<AppUser>
{
public AppUserMap()
{
Table("AppUser");
Schema("dbo");
Lazy(true);
Id(x => x.AppUserId, map => map.Generator(Generators.GuidComb));
//some omitted properties
Bag(x => x.AppRoles,
colmap => {
colmap.Cascade(Cascade.None);
colmap.Table("AppUser_AppRole");
colmap.Key(x => x.Column("AppUserId"));
},
map => map.ManyToMany(many => many.Column("AppRoleId")));
}
}
AppUser_AppRole
~~~Table~~~
AppUserId, uniqueidentifier, not null
AppRoleId, uniqueidentifier, not null
AppRole
~~~Table~~~
AppRoleId, uniqueidentifier, not null
//some omitted properties
~~~实体~~~
public class AppRole{
public virtual Guid AppRoleId { get; set; }
//some omitted properties
public virtual IList<AppUser> AppUsers { get; set; }
public virtual IList<AppPrivilege> AppPrivileges { get; set; }
}
~~~映射~~~
public class AppRoleMap : ClassMapping<AppRole> {
public AppRoleMap()
{
Table("AppRole");
Schema("dbo");
Lazy(true);
Id(x => x.AppRoleId, map => map.Generator(Generators.GuidComb));
//some omitted properties
Bag(x => x.AppUsers,
colmap => {
colmap.Cascade(Cascade.None);
colmap.Table("AppUser_AppRole");
colmap.Key(x => x.Column("AppRoleId"));
},
map => map.ManyToMany(many => many.Column("AppUserId")));
//The following definition produces the bug.
Bag(x => x.AppPrivileges,
colmap => {
colmap.Cascade(Cascade.None);
colmap.Table("AppRole_AppPrivilege");
colmap.Key(x => x.Column("AppRoleId"));
},
map => map.ManyToMany(many => many.Column("AppPrivilegeId")));
}
}
AppRole_AppPrivilege
~~~Table~~~
AppRoleId, uniqueidentifier, not null
AppPrivilegeId, uniqueidentifier, not null
AppPrivilege
~~~Table~~~
AppPrivilegeId, uniqueidentifier, not null
//some omitted properties
~~~实体~~~
public class AppPrivilege {
public virtual Guid AppPrivilegeId { get; set; }
//some omitted properties
public virtual IList<AppRole> AppRoles { get; set; }
}
~~~映射~~~
public class AppPrivilegeMap: ClassMapping<AppPrivilege> {
public AppPrivilegeMap()
{
Table("AppPrivilege");
Schema("dbo");
Lazy(true);
Id(x => x.AppPrivilegeId, map => map.Generator(Generators.GuidComb));
//some omitted properties
Bag(x => x.AppRoles,
colmap => {
colmap.Cascade(Cascade.None);
colmap.Table("AppRole_AppPrivilege");
colmap.Key(x => x.Column("AppPrivilegeId"));
},
map => map.ManyToMany(many => many.Column("AppRoleId")));
}
}
在上述文件中找不到此问题的答案。我只是忘记在 NHibernateHelper.OnConfigure()
期间将 AppPrivilegeMap
添加到 ModelMapper
。
现在最后的任务是找出.Inverse(true)
的放置位置,这样序列化一个对象就不会因为循环引用而导致异常了。