簡體   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