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?
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.