C#Entity Framework 4.1:在查询中包含用于加载相关对象的路径

时间:2011-10-21 14:07:45

标签: linq entity-framework-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;
     }

我收到此错误:

  

无法转换类型为'System.Data.Entity.Infrastructure.DbQuery 1[ClassLibrary1.Company]' to type 'System.Data.Entity.DbSet 1 [ClassLibrary1.Company]'的对象。

编译时我没有错误。在EF 4.0中,此代码运行正确,而不是使用DbSet&lt;&gt;,ObjectQuery&lt;&gt;。

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

感谢。

3 个答案:

答案 0 :(得分: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;
 }

答案 1 :(得分:1)

DbSet继承自DbQuery,因此编译器不会抱怨,因为强制转换可能有效。显然,DbSet<T>.Include返回的不是DbSet<T>,并且演员在运行时失败。

但是,你不需要施放;调用FirstOrDefault将适用于DbQuery<T>

答案 2 :(得分:0)

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

    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;
    }