【发布时间】:2018-02-07 09:21:35
【问题描述】:
在我的项目中,我遇到了一对多引用的问题。
实际上我引用了三个 dto 来创建多对多引用:
- 用户一对多角色组
- 角色一对多角色组
一个用户可以拥有多个角色,因此一个角色可以分配给更多用户。
问题: 当我尝试按标准检索用户时(在 Fluent NHibernate 中按示例搜索),我发现重复的行(实际上我的数据库中有 1 个用户用于测试 2 个角色,当我搜索此用户时,我看到 2 个相同的行)。
但是如果我尝试通过它的键检索用户,我发现了一行(这是正确的)。
下面你可以阅读 User 和 RoleGroup 的映射类,然后是用于搜索的方法......你能帮我找到问题并给我一些建议吗?
非常感谢大家! 纳克
映射:
//USER MAPPING
public class UserMap : ClassMap<User>
{
public UserMap
{
this.Schema("[dbo]");
this.Table("[users]");
this.CompositeId(x => x.Key)
.KeyProperty(x => x.ApplicationId)
.KeyProperty(x => x.Id);
this.Map(x => x.Email);
this.Map(x => x.Password);
this.Map(x => x.Pin);
this.Map(x => x.FirstName);
this.Map(x => x.SecondName);
this.Map(x => x.IsActive);
this.Map(x => x.Expiring);
this.Map(x => x.DateOfBirth);
this.Map(x => x.Phone);
this.HasMany(x => x.RoleGroups)
.Not.LazyLoad()
.Cascade.AllDeleteOrphan()
.Fetch.Join()
.Inverse()
.KeyColumns.Add("applicationId", "userId");
}
}
//ROLE GROUP MAPPING
public class RoleGroupMap: ClassMap<RoleGroup>
{
public RoleGroupMap
{
this.Schema("[dbo]");
this.Table("[role_groups]");
this.CompositeId(x => x.Key)
.KeyProperty(x => x.ApplicationId)
.KeyProperty(x => x.Id);
this.Map(x => x.UserId);
this.Map(x => x.RoleId);
this.References(x => x.User)
.Not.LazyLoad()
.Not.Nullable()
.Cascade.SaveUpdate()
.Columns("applicationId", "userId");
this.References(x => x.Role)
.Not.LazyLoad()
.Not.Nullable()
.Cascade.SaveUpdate()
.Columns("applicationId", "roleId");
}
}
//FLUENT NHIBERNATE REPO
public class Repository<TIdentifier> : IRepository<TIdentifier> where TIdentifie : class, new()
{
...
//method used to search by criteria, in this particular case Item is type of User and it is used as criteria
public virtual IEnumerable<TIdentifier> LoadByCriteria(TIdentifier Item, int? RecordStartIndex = null, int? TotalRecords = null)
{
IList<Tidentifier> itemList;
using(ISession session = NHibernateConfiguration<TIdentifier>.OpenSession())
{
ICriteria criteria = session.CreateCriteria<TIdentifier>();
if(RecordStartIndex != null)
{
criteria.SetFirstResult((int)RecordStartIndex);
}
if(TotalRecords != null)
{
criteria.SetMaxResults((int)TotalRecords);
}
itemList = criteria.Add(Example.Create(Item))
.List<TIdentifier>();
}
if(itemList == null)
itemList = new List<TIdentifier>();
return itemList.ToList();
}
}
【问题讨论】:
标签: .net nhibernate fluent-nhibernate nhibernate-mapping fluent-nhibernate-mapping