2

我正在輪詢安全信息的多個系統(域),所以我正在處理domainUsers及其角色。我有我的實體設置如下所示,但我無法在AutoMapper覆蓋中設置domainUser.HasMany關係。如何覆蓋複合標識的自動映射,與流暢的nhibernate一對多關係?

你會注意到我沒有domainUser.DomainUserId和role.RoleId這使得這更簡單(沒有compositeIds。)我已經避免了這些字段,因爲我已經有了一個自然的組合鍵,它當我從下游域中獲取這些數據時將會填充。如果我添加這些人造鍵,那麼在調用session.Merge(domainUser)之前,我必須預先獲取它們的值。我試圖避免這樣做。

實體對象是顯而易見的(我希望),但這裏是我得到的。

public class DomainUser 
    { 
     public virtual int Domain_Id { get; set; } 
     public virtual string DomainUserLogin { get; set; } 
     public virtual string EmployeeId { get; set; } 

     // extra field removed for breviety 


     public DomainUser() 
     { 
     this.Roles = new List<DomainUserRole>(); 
     } 

     public virtual void AddRole(DomainUserRole role) 
     { 
     role.DomainUser = this; 
     this.Roles.Add(role); 
     } 

     // overrides for equals and getHashCode 
    } 

public class DomainUserRole 
    { 
     public virtual DomainUser DomainUser { get; set; } 
     public virtual string DataSegment { get; set; } // Some group of data a user has access to, like US or China 
     public virtual string RoleName { get; set; } 
     public virtual string RoleDescription { get; set; } 

     // extra field removed for breviety 

     // overrides for equals and getHashCode 
    } 

我的數據庫架構是非常簡單的。

alt text http://lh6.ggpht.com/_MV6QGBD11JE/S3iX2qcP_jI/AAAAAAAAEE0/PGIO07BlCSo/s800/Untitled.gif.jpg

我已經得到了IAutoMappingOverride類開始是這樣的。但是,我不知道如何爲角色設置hasMany。它一直給我

NHibernate.FKUnmatchingColumnsException: 
    Foreign key (FK20531BE4163641BB:tblDomainUserRoles [DomainUser])) 
    must have same number of columns as the referenced primary key 
    (tblDomainUsers [Domain_Id, DomainUserLogin]). 

如何設置外鍵使用這兩個字段?

public class DomainUserMap : IAutoMappingOverride<DomainUser> 
    { 
     public void Override(AutoMapping<DomainUser> mapping) 
     { 
     mapping.CompositeId() 
      .KeyProperty(user => user.Domain_Id, "Domain_Id") 
      .KeyProperty(user => user.DomainUserLogin, "DomainUserLogin"); 

     // I"ve tried this. 
     // mapping.HasMany(x => x.Roles) 
     // .KeyColumns.Add("Domain_Id") 
     // .KeyColumns.Add("DomainUserLogin"); 

     // also tried this where I define this FK in DB with both fields. 
     // mapping.HasMany(x => x.Roles) 
     // .ForeignKeyConstraintName("FK_tblDomainUserRoles_tblDomainUsers") 

     } 
    } 

    public class DomainUserRoleMap : IAutoMappingOverride<DomainUserRole> 
     { 
      public void Override(AutoMapping<DomainUserRole> mapping) 
      { 
      mapping.CompositeId() 
       .KeyReference(role => role.DomainUser) 
       .KeyProperty(role => role.DataSegment) 
       .KeyProperty(role => role.RoleName);   
      } 
     } 

回答

0

我做了最終的手編輯HBM文件。看起來最新的流利Nhibernate版本解決了這個問題。這是我的hbm文件的樣子。

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true"> 
    <class xmlns="urn:nhibernate-mapping-2.2" name="AAA.Core.Entities.DomainUser, AAA.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`tblDomainUsers`"> 
    <composite-id mapped="false" unsaved-value="undefined"> 
     <key-property name="Domain_Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
     <column name="Domain_Id" /> 
     </key-property> 
     <key-property name="DomainUserLogin" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
     <column name="DomainUserLogin" /> 
     </key-property> 
    </composite-id> 
    ... properties hidden for breviety 
    <bag inverse="true" cascade="all-delete-orphan" lazy="false" name="Roles"> 
     <key> 
     <column name="Domain_Id" /> 
     <column name="DomainUserLogin" /> 
     </key> 
     <one-to-many class="AAA.Core.Entities.DomainUserRole, AAA.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> 
    </bag> 
    </class> 
</hibernate-mapping> 

這裏是角色的文件。

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true"> 
    <class xmlns="urn:nhibernate-mapping-2.2" name="AAA.Core.Entities.DomainUserRole, AAA.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`tblDomainUserRoles`"> 
    <composite-id mapped="false" unsaved-value="undefined"> 
     <key-property name="DataSegment" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
     <column name="DataSegment" /> 
     </key-property> 
     <key-property name="RoleName" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
     <column name="RoleName" /> 
     </key-property> 
     <key-many-to-one name="DomainUser" class="AAA.Core.Entities.DomainUser, AAA.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"> 
     <column name="Domain_Id" /> 
     <column name="DomainUserLogin" /> 
     </key-many-to-one> 
    </composite-id> 
    ... properties hidden for breviety 
    </class> 
</hibernate-mapping> 
0

您可能想要使用組合鍵類。

使用密鑰類here at SO的示例。但是,您可能必須下拉並使用HBM文件,而不是使用Fluent NHibernate。

相關問題