NHibernate延迟加载获取方法

时间:2019-12-02 11:09:14

标签: c# sql asp.net asp.net-mvc nhibernate

我在c#中的nhibernate的获取方法中有一个问题。 提取方法不起作用,但是在同一个项目中,我没有任何问题,它可以正常工作。 为什么?

这是我的模特:

组织班级:

public class Organization : IModel, IOrganization
{
    public virtual Guid Id { get; set; }
    public virtual Guid? ParentId { get; set; }
    public virtual IOrganization Parent { get; set; }
    public virtual List<IOrganization> Childs { get; set; }
    public virtual string Name { get; set; }
    public virtual Guid OrgTypeId { get; set; }
    public virtual Guid OrgTypeId { get; set; }
    public virtual OrganizationType OrgType { get; set; }
    public virtual List<IPosition> Positions { get; set; }
    public virtual IOrganizationChartVersion Version { get; set; }
    public virtual string Description { get; set; }
    public virtual int Capacity { get; set; }
    public virtual string Image { get; set; }
    public virtual ICompany Company { get; set; }
    public virtual void AddChild(IOrganization org)
    {
        org.Parent.Id = this.Id;
        if (Childs == null)
        {
            Childs = new List<IOrganization>();
        }

        Childs.Add(org);
    }

    public virtual List<IOrganization> AssociateWithChilds(ISession session, List<IOrganization> lstOrg = null)
    {
        List<IOrganization> organizations = new List<IOrganization>();
        if (lstOrg == null)
            lstOrg = session.Query<IOrganization>().Where(o => o.Parent != null).ToList();

        organizations = this.findChilds(lstOrg, this);
        return organizations;
    }

    private List<IOrganization> findChilds(List<IOrganization> orgs, IOrganization org)
    {
        List<IOrganization> organizations = new List<IOrganization> { org };
        var childs = orgs.Where(o => o.Parent.Id == org.Id);
        org.Childs = childs.ToList();
        if (childs.Any())
        {
            foreach (var organization in childs)
            {

                organizations.AddRange(this.findChilds(orgs, organization));
            }
        }
        return organizations;
    }

    public virtual List<IOrganization> AssociateToRoot(ISession session)
    {
        List<IOrganization> organizations = new List<IOrganization>();
        // var orgs = session.Query<IOrganization>().Where(o => o.Parent != null).ToList();
        organizations = this.FindTopsToRoot(this);
        return organizations;
    }

    private List<IOrganization> FindTopsToRoot(IOrganization org)
    {
        List<IOrganization> organizations = new List<IOrganization> { org };
        if (org.Parent != null)
        {
            organizations.AddRange(this.FindTopsToRoot(org.Parent));
        }
        return organizations;
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj))
        {
            return false;
        }
        if (ReferenceEquals(this, obj))
        {
            return true;
        }
        if (obj.GetType() != GetType() && obj.GetType().BaseType != GetType())
        {
            return false;
        }
        return Equals((Organization)obj);
    }

    protected bool Equals(Organization other)
    {
        return Id.Equals(other.Id);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            int hashCode = Id.GetHashCode();
            return hashCode;
        }
    }
}

公司类别:

public class Company : IModel, ICompany
{
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
    public virtual int? LocaleId { get; set; }
    public virtual IOrganizationChartVersion Version { get; set; }
    public virtual Guid CreatedBy { get; set; }
    public virtual DateTime CreatedDate { get; set; }
    public virtual Guid? ModifiedBy { get; set; }
    public virtual DateTime? ModifiedDate { get; set; }
    public virtual string Color { get; set; }
    public virtual IEnumerable<IOrganization> Organizations { get; set; }
}

组织映射:

public class OrganizationMap : ClassMap<Organization>
{
    public OrganizationMap()
    {
        var kernel = BaseKernelSingleton.CreateBaseKernelSingleton().ReturnBaseKernel();
        Id(x => x.Id).GeneratedBy.Assigned();
        Map(x => x.Name);
        Map(x => x.Capacity);
        References(x => x.OrgType, "OrgTypeId").Cascade.None();
        var orgparent = kernel.Get<IOrganization>().GetType();
        References(x => x.Parent, "ParentId").Class(orgparent).Cascade.None().LazyLoad();
        HasMany<Organization>(x => x.Childs).KeyColumn("ParentId").Inverse().Cascade.Delete().LazyLoad();
        References(x => x.Version, "VersionId").Class(typeof(OrganizationChartVersion)).Cascade.None();
        var company = kernel.Get<ICompany>().GetType();
        References(x => x.Company, "CompanyId").Class(company).Cascade.None().LazyLoad();
        Map(x => x.Description);
        Map(x => x.Image);

        LazyLoad();
    }
}

公司映射:

public class CompanyMap : ClassMap<Company>
{
    public CompanyMap()
    {
        Table("Company");
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.CreatedBy);
        Map(x => x.CreatedDate);
        Map(x => x.LocaleId);
        Map(x => x.ModifiedBy);
        Map(x => x.ModifiedDate);
        Map(x => x.Color);
        References(x => x.Version).Class(typeof(OrganizationChartVersion)).Column("VersionId");
    }
}

这是我的查询:

var query = (from org in session.Query<Organization>()
                             select new Organization
                             {
                                 Id = org.Id,
                                 Capacity = org.Capacity,
                                 Childs = org.Childs,
                                 Name = org.Name,
                                 Description = org.Description,
                                 Parent = org.Parent,
                                 Version = org.Version
                             });
            var lst = query.Fetch(x => x.Company).ToList();

异常消息:

System.ArgumentException: 'Expression of type 'System.Collections.IList' cannot be used for parameter of type 'System.Collections.Generic.IEnumerable`1[System.Object]

我在其他项目中具有相同的代码,并且可以正常工作。 NHibernate的版本是5.2.6。

0 个答案:

没有答案