Fluent NHibernate:具有给定类标识符的多行
这个问题实际上和我之前的一个问题有关。
我有一个数据库表 TItemListUserPerm ,其复合主键由三个字段组成。
TItemListUserPerm
- UserId (PK,FK)
- TItemListID (PK,FK)
- TItemListPermID (PK,FK)
在努力解决如何映射此问题后,我得出的结论是,最简单的事情是删除复合主键,添加唯一的标识主键,并将 TItemListUserPerm 变成一个我可以映射的实体。
结果是:
public class TaskItemListUserPermission {
public int Id { get; set; }
public User User { get; set; }
public TaskItemList TaskItemList { get; set; }
public TaskItemListPermission Permissions{ get; set; }
}
地图是:
public class TaskItemListUserPermissionMap : ClassMap<TaskItemListUserPermission> {
public TaskItemListUserPermissionMap() {
Table("TtemListUserPerm");
Id(x => x.Id, "TaskItemListUserPermId");
References(x => x.User, "UserId");
References(x => x.Permissions, "TItemListPermID");
References(x => x.TaskItemList, "TItemListID");
}
}
public class TaskItemListMap : ClassMap<TaskItemList> {
Table("TItemList");
Id(x => x.Id, "TItemListID");
....
HasMany<TaskItemListUserPermission>(x => x.UserPermssions)
.Table("TItemListUserPerm")
.KeyColumn("TItemListId")
.AsBag();
}
一切都很好。我得到结果;不完全是我想要的,但我认为我可以使用它,直到我编写像这样的 Linq 查询:
List<TaskItemList> taskItemLists = taskItemListRepository.GetAll() as List<TaskItemList>;
List<TaskItemListUserPermission> permissions = taskItemLists.First().TaskItemListUserPermission.ToList();
var myName= permissions.Where<TaskItemListUserPermission>(x => x.User.FirstName == "FirstNameOfUser" && x.User.LastName == "LastNameOfUser");
当我单步执行代码时,我收到一个错误:找到了不止一行给定的标识符:IdOfUser,for对象的类名。
我知道我得到了一组具有相同标识符的用户。它们是可枚举列表中实体的一部分。谁能解释一下为什么我会收到错误以及如何修复或解决它?我需要使用 Linq-To-NHibernate 吗?
This question is actually related to a previous question that I had.
I had a database Table TItemListUserPerm with a composite primary key consisting of three fields.
TItemListUserPerm
- UserId (PK,FK)
- TItemListID (PK,FK)
- TItemListPermID (PK,FK)
After struggling with how to map this, I came to the conclusion that the simplest thing would be to remove the composite primary key, add a unique, identity primary key, and make TItemListUserPerm into an entity which I could then map.
The result was:
public class TaskItemListUserPermission {
public int Id { get; set; }
public User User { get; set; }
public TaskItemList TaskItemList { get; set; }
public TaskItemListPermission Permissions{ get; set; }
}
The map was:
public class TaskItemListUserPermissionMap : ClassMap<TaskItemListUserPermission> {
public TaskItemListUserPermissionMap() {
Table("TtemListUserPerm");
Id(x => x.Id, "TaskItemListUserPermId");
References(x => x.User, "UserId");
References(x => x.Permissions, "TItemListPermID");
References(x => x.TaskItemList, "TItemListID");
}
}
public class TaskItemListMap : ClassMap<TaskItemList> {
Table("TItemList");
Id(x => x.Id, "TItemListID");
....
HasMany<TaskItemListUserPermission>(x => x.UserPermssions)
.Table("TItemListUserPerm")
.KeyColumn("TItemListId")
.AsBag();
}
Everything works great. I get results; not exactly what I want, but something I think that I can work with until I write Linq queries like this:
List<TaskItemList> taskItemLists = taskItemListRepository.GetAll() as List<TaskItemList>;
List<TaskItemListUserPermission> permissions = taskItemLists.First().TaskItemListUserPermission.ToList();
var myName= permissions.Where<TaskItemListUserPermission>(x => x.User.FirstName == "FirstNameOfUser" && x.User.LastName == "LastNameOfUser");
When I'm stepping through the code, I get an error: More than one row of given identifier was found: IdOfUser, for ClassNameOfObject.
I know that I'm getting a group of Users with the same identifier. They are part of an entity that is in an enumerable list. Can anyone shed some light on why I'm getting the error and how to either fix or work around it? Do I need to use Linq-To-NHibernate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题最终是类不匹配,特别是 User 类。我通过注释掉类映射中的引用来缩小源范围,直到没有收到错误为止。一旦我这样做了,我就能推断出这个问题。我感谢任何看过这个问题并试图回答它的人。
The problem ultimately ended up being a mismatch in classes, specifically the User class. I narrowed down the source by commenting out the References in the class map until no errors received. Once I did that, I was able to deduce the problem. I appreciate anyone who looked at this question and attempted to answer it.