繁体   English   中英

流利的NHibernate ManyToMany与参考表

[英]Fluent NHibernate ManyToMany with Reference Table

在采取了许多解决方案的几种解决方案后,我无法使此东西正常工作
我有这些表:

User:  
intID (PK)  
vcUsername

Role:  
intID (PK)  
vcDescription

UserRole:  
intID (PK-FK)  
intRole (PK-FK)  
btActive

这是我的课:

public class User {
  public virtual int Id {get; set;}
  public virtual string Username {get;set;}
  public virtual IList<UserRole> Roles {get; set;}
}

public class Role {
  public virtual int Id {get; set;}
  public virtual string Description {get;set;}
  public virtual IList<UserRole> Users {get; set;}
}

public class UserRole {
  public virtual User User {get; set;}
  public virtual Role Role {get;set;}
  public virtual bool IsActive {get; set;}
}

这是我的班级地图:

public UserMap() {
  Table("tb_user");
  Id(f => f.Id).Column("intID").GeneratedBy.Native();
  Map(f => f.Username).Column("vcUsername").Not.Nullable();
  HasMany(f => f.Roles).KeyColumn("intID").LazyLoad().Inverse().Cascade.All();
}

public RoleMap() {
  Table("tb_role");
  Id(f => f.Id).Column("intID").GeneratedBy.Native();
  Map(f => f.Description).Column("vcDescription").Not.Nullable();
  HasMany(f => f.Roles).KeyColumn("intRole").LazyLoad();
}

public UserRoleMap()
{
  Table("tb_user_role");
  References(f => f.User).Column("intID").Not.Nullable();
  References(f => f.Role).Column("intRole").Not.Nullable();
  Map(f => f.IsActive).Column("btActive").Not.Nullable();
}

当我运行时,我在启动时收到此错误
The entity 'UserRole' doesn't have an Id mapped

我如何正确地映射此多体,插入和更新工作正常? 我希望你的向导清楚..谢谢

可能应该是这样:

public UserRoleMap()
{
  Table("tb_user_role");
  CompositeId()
            .KeyReference(x => x.User, "intID")
            .KeyReference(x => x.Role, "intRole");
  Map(f => f.IsActive).Column("btActive").Not.Nullable();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM