2011-10-21 41 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; 
    }