gpt4 book ai didi

nhibernate - NHibernate的复合键/ID映射

转载 作者:行者123 更新时间:2023-12-03 13:36:18 25 4
gpt4 key购买 nike

我的数据库中有以下表格:

Announcements:
- AnnouncementID (PK)
- Title

AnouncementsRead (composite PK on AnnouncementID and UserID):
- AnnouncementID (PK)
- UserID (PK)
- DateRead

Users:
- UserID (PK)
- UserName


通常,我会使用多对多关系来映射“ AnnouncementsRead”,但此表还具有一个附加的“ DateRead”字段。

到目前为止,我已经定义了以下实体:

    public class Announcement
{
public virtual int AnnouncementID { get; set; }
public virtual string Title { get; set; }
public virtual IList<AnnouncementRead> AnnouncementsRead { get; private set; }

public Announcement()
{
AnnouncementsRead = new List<AnnouncementRead>();
}
}

public class AnnouncementRead
{
public virtual Announcement Announcement { get; set; }
public virtual User User { get; set; }
public virtual DateTime DateRead { get; set; }
}

public class User
{
public virtual int UserID { get; set; }
public virtual string UserName { get; set; }
public virtual IList<AnnouncementRead> AnnouncementsRead { get; private set; }

public User()
{
AnnouncementsRead = new List<AnnouncementRead>();
}
}


使用以下映射:

public class AnnouncementMap : ClassMap<Announcement>
{
public AnnouncementMap()
{
Table("Announcements");
Id(x => x.AnnouncementID);
Map(x => x.Title);
HasMany(x => x.AnnouncementsRead)
.Cascade.All();
}
}

public class AnnouncementReadMap : ClassMap<AnnouncementRead>
{
public AnnouncementReadMap()
{
Table("AnnouncementsRead");
CompositeId()
.KeyReference(x => x.Announcement, "AnnouncementID")
.KeyReference(x => x.User, "UserID");
Map(x => x.DateRead);
}
}

public class UserMap : ClassMap<User>
{
public UserMap()
{
Table("Users");
Id(x => x.UserID);
Map(x => x.UserName);
HasMany(x => x.AnnouncementsRead)
.Cascade.All();
}
}


但是,当我运行此程序时,出现以下错误:

"composite-id class must override Equals(): Entities.AnnouncementRead"


如果有人能指出我正确的方向,我将不胜感激。谢谢

最佳答案

您应该执行NHibernate告诉您的操作。 AnnouncementRead应该覆盖EqualsGetHashCode方法。它们应基于作为主键一部分的字段

关于nhibernate - NHibernate的复合键/ID映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3439155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com