2011-10-21 100 views
2

当我运行从这个方​​法这行代码C#实体框架4.1:包括在查询加载相关对象

queryCompanies = (DbSet)queryCompanies.Include(path);

路径:

 public Company GetCompanyById(int companyId) 
    { 
     List<string> includePaths = new List<string>(); 
     includePaths.Add("Addresses"); 
     includePaths.Add("Users"); 
     Company company = null; 
     using (Entities dbContext = new Entities()) 
     { 
      var queryCompanies = dbContext.Companies; 

      if (includePaths != null) 
      { 
       foreach (string path in includePaths) 
        queryCompanies = (DbSet<Company>)queryCompanies.Include(path); 
      } 

       company = (from c in queryCompanies 
          where c.Id.Equals(companyId) 
          select c).FirstOrDefault<Company>(); 
     } 
     return company; 
    } 

我得到这个错误:

Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery 1[ClassLibrary1.Company]' to type 'System.Data.Entity.DbSet 1[ClassLibrary1.Company]'.

编译时我没有错误。在EF 4.0中,此代码运行正确,而不是DbSet <>,ObjectQuery <>。

我是EF 4.1的初学者,所以任何建议都会有用。

谢谢。

回答

4

试试这个

public Company GetCompanyById(int companyId) 
{ 
    List<string> includePaths = new List<string>(); 
    includePaths.Add("Addresses"); 
    includePaths.Add("Users"); 
    Company company = null; 
    using (Entities dbContext = new Entities()) 
    { 
     var queryCompanies = dbContext.Companies.AsQueryable(); 

     if (includePaths != null) 
     { 
      foreach (string path in includePaths) 
       queryCompanies = queryCompanies.Include(path); 
     } 

      company = (from c in queryCompanies 
         where c.Id.Equals(companyId) 
         select c).FirstOrDefault<Company>(); 
    } 
    return company; 
} 
+0

您不需要'AsQueryable'。它只会没有工作。 – Slauma

+0

cadrell0,感谢您的答案,它适用于您的解决方案。 – Cargo

1

DbSet继承DbQuery,所以编译器不会抱怨,因为剧组可以是有效的。显然,DbSet<T>.Include返回的不是DbSet<T>,并且在运行时转换失败。

但是,您不需要投射;调用FirstOrDefault将工作在DbQuery<T>

+0

问题是queryCompanies的类型是DbSet 。 for循环,他添加包含不能将DbQuery 设置为DbSet ,因此他添加了演员。将dbContext.Companies转换为可查询(使用AsQueryable)将不需要投射包含的结果。 – cadrell0

0

我写了一个通用的include检查器方法。现在这不好,但它是工作。我会重构这个。也许它符合你的需求。

List<TEntityType> GetEntityListTemplate(Expression<Func<TEntityType, bool>> expression = null) 
    { 
     List<TEntityType> entityList; 
     DbQuery<TEntityType> query = null; 

     Type entityType = typeof(TEntityType); 
     PropertyInfo[] properties = entityType.GetProperties(); 

     using (DatabaseContext database = new DatabaseContext()) 
     { 
      database.Database.Connection.Open(); 

      foreach (PropertyInfo property in properties) 
      { 
       if (property.PropertyType.FullName.Contains("Your.Identifier.Namespace")) 
       { 
        if (query == null) 
        { 
         query = database.Set<TEntityType>().Include(property.Name); 
        } 
        else 
        { 
         query = query.Include(property.Name); 
        } 
       } 
      } 

      if (query == null) 
      { 
       if (expression == null) 
       { 
        entityList = database.Set<TEntityType>().ToList(); 
       } 
       else 
       { 
        entityList = database.Set<TEntityType>().Where(expression).ToList(); 
       } 
      } 
      else //(query!=null) 
      { 
       if (expression == null) 
       { 
        entityList = query.ToList(); 
       } 
       else 
       { 
        entityList = query.Where(expression).ToList(); 
       } 
      } 
     } 

     return entityList; 
    }