Fluent / Nhibernate:无法检索快照

时间:2015-04-17 09:24:35

标签: c# nhibernate fluent-nhibernate automapper

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

  

{"无法检索快照:   [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仅将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 个答案:

答案 0 :(得分:1)

我自己发现了这个问题。我收到错误,因为联结表的名称不正确。它假设是 AccountsGroupRelation 而不是 AccountsGroup_Accounts

我修好后,一切都开始工作了。