1

I can't understand what i'm doing wrong. Every time I'm getting this error:

The entity or complex type 'BusinessLogic.CompanyWithDivisionCount' cannot be constructed in a LINQ to Entities query.

I need to get info from 'Company' table and divisions count of each company from 'Division' table, and then make PagedList. Here is my 'Company' table:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;

using BusinessLogic.Services;
using BusinessLogic.Models.ValidationAttributes;

namespace BusinessLogic.Models
{
    public class Company
    {
        public Company()
        {
            Country = "US";
            Status = true;
        }

        public int Id { get; set; }

        [Required]
        [UniqueCompanyName]
        public string Name { get; set; }

        public string Street { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public int Zip { get; set; }
        public string Country { get; set; }

        public string ContactInfo { get; set; }

        [Required]
        public DateTime EffectiveDate { get; set; }
        public DateTime TerminationDate { get; set; }

        public bool Status { get; set; }

        [Required]
        public string URL { get; set; }
        public string EAP { get; set; }

        public string EAPCredentials { get; set; }

        public string BrandingColors { get; set; }

        public string Comments { get; set; }
    }
}

Here is my domain model:

public class Company
{
    public Company()
    {
        Country = "US";
        Status = true;
    }

    public int Id { get; set; }

    [Required]
    [UniqueCompanyName]
    public string Name { get; set; }

    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public int Zip { get; set; }
    public string Country { get; set; }

    public string ContactInfo { get; set; }

    [Required]
    public DateTime EffectiveDate { get; set; }
    public DateTime TerminationDate { get; set; }

    public bool Status { get; set; }

    [Required]
    public string URL { get; set; }
    public string EAP { get; set; }

    public string EAPCredentials { get; set; }

    public string BrandingColors { get; set; }

    public string Comments { get; set; }
}

public class CompanyWithDivisionCount: Company // I'm using this
{
    public int DivisionCount { get; set; }
}

Here is my controller:

public ActionResult CompaniesList(int? page)
{
    var pageNumber = page ?? 1;

    var companies = companyService.GetCompaniesWithDivisionsCount2();

    var model = companies.ToPagedList(pageNumber, PageSize);

    return View(model);
}

And here is my service part:

public IQueryable<CompanyWithDivisionCount> GetCompaniesWithDivisionsCount2()
{
    return (from c in dataContext.Companies.AsQueryable()
            select new CompanyWithDivisionCount
            {
                Id = c.Id,
                Name = c.Name,
                Status = c.Status,
                EffectiveDate = c.EffectiveDate,
                URL = c.URL,
                EAP = c.EAP,
                EAPCredentials = c.EAPCredentials,
                Comments = c.Comments,
                DivisionCount = (int)dataContext.Divisions.Where(b => b.CompanyName == c.Name).Count()
            });
}

}

Thanks for help!!!

4

2 回答 2

1

PagedList 的创建者在这里。这与 PagedList 无关,而是实体框架问题(我不是实体框架方面的专家,因此无法帮助您)。要确认这是真的,请按照以下几行编写单元测试:

[Test]
public void ShouldNotThrowAnException()
{
    //arrange
    var companies = companyService.GetCompaniesWithDivisionsCount2();

    //act
    var result = companies.ToList();

    //assert
    //if this line is reached, we win! no exception on call to .ToList()
}
于 2012-08-09T12:03:45.707 回答
0

如果可能,我会考虑更改您的数据模型,而不是通过名称字符串将公司与部门相关联,而是使用两个对象之间正确维护的外键关系(部门应包含 CompanyID 外键)。这有很多好处(包括性能和数据完整性),并且如果您需要对您的应用程序进行进一步更改(或者如果任何公司决定重新命名它的名称),几乎肯定会让您的生活更轻松。

如果您创建正确的外键关系,那么您的域模型可能看起来像

public class Company
{
    ...

    public virtual ICollection<Division> Divisions{ get; set; }

    public int DivisionCount
    {
         get 
         {
              return this.Divisions.Count()
         }
    }

    ...
}
于 2012-08-09T09:33:54.503 回答