2015-04-17 70 views
0

我想映射與流利NHibernate的多對多關係,但它不適合我。我正在用AutoMapper映射實體。我嘗試繪製時出現此錯誤;流利/ Nhibernate:無法檢索快照

{「無法檢索快照: [Models.AccountsGroup_Accounts#Models.AccountsGroup_Accounts] [SQL: SELECT chartofacc_.fkGroupID,chartofacc_.fkAccountID FROM AccountsGroup_Accounts chartofacc_ WHERE chartofacc_.fkGroupID =和chartofacc_.fkAccountID? =?]「}

表是;

1. Accounts (table name) 
    AccountID, 
    AccountName 

2. AccountsGroup (table name) 
    AccountGroup_ID, 
    GroupName 

3. AccountsGroup_Accounts (table name) 
    fkAccountsID, 
    fkAccountsGroup_ID 

表AccountsGroup_Accounts僅持有帳戶和AccountsGroup表的主鍵那樣forigen按鍵也會使複合鍵從他們的本身。

模型類是;

public class Account 
     { 
      public virtual int Id { get; set; } 
      public virtual string AccountName { get; set; } 
      public virtual IList<AccountGroup> AccountGroups { get; set; } 
     } 
public class AccountGroup 
    { 
     public virtual int Id { get; set; } 
     public virtual string GroupName { get; set; } 
     public virtual IList<Account> Accounts { get; set; } 
    } 

public class AccountsGroup_Accounts 
    { 
     public virtual Account Accounts { get; set; } 
     public virtual AccountGroup AccountGroups { get; set; } 

     public override bool Equals(object obj) 
     { 
      if (obj == null) 
       return false; 
      var compare = obj as AccountsGroup_Accounts; 
      if (compare == null) 
       return false; 

      return (Accounts.Id == compare.Accounts.Id && AccountGroups.Id == compare.AccountGroups.Id); 
     } 
     public override int GetHashCode() 
     { 
      return (Accounts.Id + "|" + AccountGroups.Id).GetHashCode(); 
     } 
    } 

映象是;

public AccountGroupMapping() 
     { 
      SetEntityProperties("AccountGroups", "Group_ID"); 
      Map(x => x.GroupName); 
      HasManyToMany<Account>(x => x.Accounts) 
      .Table("AccountsGroup_Accounts") 
      .ParentKeyColumn("fkGroupID") 
      .ChildKeyColumn("fkAccountID") 
      .LazyLoad(); 
     } 
public AccountMapping() 
     { 
      SetEntityProperties("Accounts", "AccountID"); 
      Map(x => x.AccountName); 
      HasManyToMany<AccountGroup>(x => x.AccountGroup) 
      .Table("AccountsGroup_Accounts") 
      .ParentKeyColumn("fkAccountID") 
      .ChildKeyColumn("fkGroupID") 
      .LazyLoad(); 
     } 
public AccountsGroup_Accounts() 
     { 
      Table("AccountsGroup_Accounts"); 
      CompositeId().KeyReference(x => x.AccountGroup, "fkGroupID") 
       .KeyReference(x => x.Account, "fkAccountID"); 
     } 

非模型類(這些是從xsd2code生成的);

public partial class Accounts 
    { 
     public int AccountIdField {get; set;} 
     public string accountNameField {get; set;} 
    } 
public partial class AccountGroup 
    { 
     public int accountGroupIdField {get; set;} 
     public string groupNameField {get; set;} 
    } 
public partial class AccountsGroup_Accounts 
    { 
     public int AccountGroupsRefId {get; set;} 
     public int AccountsRefId { get;set;} 
    } 

這就是我如何將它們映射:

AutoMapper.Mapper.CreateMap<Account, Models.Account>() 
       .ForMember(x => x.Vat, opt => opt.ResolveUsing(new VatRefIdResolver(loadRepository)).FromMember(x => x.VatRefId)); 
AutoMapper.Mapper.CreateMap<AccountGroup, Models.AccountGroup>(); 

AutoMapper.Mapper.CreateMap<AccountsGroup_Accounts, Models.AccountsGroup_Accounts>() 
       .ForMember(x => x.Account, opt => opt.ResolveUsing(new AccountResolver(loadRepository)).FromMember(x => x.AccountRefId)) 
       .ForMember(x => x.AccountGroup, opt => opt.ResolveUsing(new AccountGroupIdResolver(loadRepository)).FromMember(x => x.AccountGroupRefId)); 

所以這是我迄今所做的。現在我不知道在什麼地方丟失了什麼,還有什麼需要去做,所以解決了這個問題或者多對多的映射。

如果有人能幫助我找出問題,我將不勝感激。

回答

1

我自己發現了這個問題。我收到錯誤,因爲聯結表的名稱不正確。它假設爲AccountsGroupRelation而不是AccountsGroup_Accounts

只要我修好它,一切開始工作。