Fluent NHibernate:具有给定类标识符的多行

发布于 2024-10-19 14:41:13 字数 1860 浏览 1 评论 0原文

这个问题实际上和我之前的一个问题有关。

我有一个数据库表 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难以启齿的温柔 2024-10-26 14:41:13

问题最终是类不匹配,特别是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文