Fluent / Nhibernate:无法检索快照

Fluent / Nhibernate: could not retrieve snapshot

我正在尝试用 Fluent NHibernate 映射多对多关系,但它对我不起作用。我正在使用 AutoMapper 映射实体。当我尝试映射时出现此错误;

{"could not retrieve snapshot: [Models.AccountsGroup_Accounts#Models.AccountsGroup_Accounts][SQL: SELECT chartofacc_.fkGroupID, chartofacc_.fkAccountID FROM AccountsGroup_Accounts chartofacc_ WHERE chartofacc_.fkGroupID=? and chartofacc_.fkAccountID=?]"}

Table是;

1. Accounts (table name)
   AccountID,
   AccountName

2. AccountsGroup (table name)
   AccountGroup_ID,
   GroupName

3. AccountsGroup_Accounts (table name)
   fkAccountsID,
   fkAccountsGroup_ID

Table AccountsGroup_Accounts 仅将 Accounts 和 AccountsGroup table 的主键保存为 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)); 

这就是我到目前为止所做的。现在我不知道我遗漏了什么以及在哪里遗漏了什么以及还需要做什么才能解决该问题或多对多映射。

如果有人能帮我找出问题,我将不胜感激。

我自己发现了问题。我收到错误是因为联结 table 的名称不正确。它应该是 AccountsGroupRelation 而不是 AccountsGroup_Accounts.

我一修复它,一切就开始工作了。