为什么这个Include不工作? [英] Why doesn't this Include work?

查看:161
本文介绍了为什么这个Include不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将值传递给一个使用foreach循环遍历集合的方法。在循环中,实体框架使用 Include 语句来加载。这是我传递的:

  var exp = new Collection< Expression&FunK< Foo,object>> ); 

为什么当我使用这个:

  exp.Add(f => f.Bars.Select(b => b.Employees.Select(e => e.Position))); 
exp.Add(f => f.Bars.Select(b => b.Employees.Select(e => e.Bank)));

和员工,职位和银行都有字段名称它会在不同字段之间混乱名称?如同银行和职位在姓名字段中都有雇员名称。要更加明确,无论何种原因,在数据库中:

  

< Employee.Name =Jon;
Employee.Bank.Name =世行;
Employee.Position.Name =CEO;

数据来自 .Include

  Employee.Bank.Name ==Jon// true 
Employee.Position.Name ==Jon// true

额外信息,接受exp

  DbSet&Foo> dbSet = context.Set&Foo>(); 
IQueryable< Foo> query = dbSet;

if(exp!= null)
{
foreach(var incProp in exp)
{
query = query.Include(incProp);
}
}

我的代码中有错误吗? p>

编辑

  public class Foo 
{
public int FooId {get;组; }
public virtual List< Bar>酒吧{get;组; }
}

public class Bar
{
public int BarId {get;组; }
public virtual Foo Foo {get;组; }
public int FooId {get;组; }
public virtual List< Employee>员工{get;组; }
}

public class Employee
{
public int EmployeeId {get;组; }
public int BarId {get;组; }
public virtual Bar Bar {get;组; }
public int BankId {get;组; }
public virtual Bank Bank {get;组; }
public int PositionId {get;组; }
public virtual Position Position {get;组; }
public string Name {get;组; }
}

public class Bank
{
public int BankId {get;组; }
public string Name {get;组;
}

public class Position
{
public int PositionId {get;组; }
public string Name {get;组; }
}


解决方案

我不认为问题出在你所显示的代码中。我从上面的内容创建了一个控制台应用程序,并输出数据库中的内容。这是整个应用程序:

 命名空间ExampleCF 
{
public class Foo
{
public int FooId {get;组; }
public virtual List< Bar>酒吧{get;组; }
}

public class Bar
{
public int BarId {get;组; }
public virtual Foo Foo {get;组; }
public int FooId {get;组; }
public virtual List< Employee>员工{get;组; }
}

public class Employee
{
public int EmployeeId {get;组; }
public int BarId {get;组; }
public virtual Bar Bar {get;组; }
public int BankId {get;组; }
public virtual Bank Bank {get;组; }
public int PositionId {get;组; }
public virtual Position Position {get;组; }
public string Name {get;组; }
}

public class Bank
{
public int BankId {get;组; }
public string Name {get;组;
}

public class Position
{
public int PositionId {get;组; }
public string Name {get;组;
}

public class Model:DbContext
{
public DbSet&Foo> Fo子组; }
public DbSet< Bar>酒吧{get;组; }
public DbSet< Employee>员工{get;组; }
public DbSet< Bank>银行{get;组; }
public DbSet< Position>位置{get;组; }

public Model()
{
Configuration.LazyLoadingEnabled = false;
}
}

类程序
{
static void Main(string [] args)
{
模型上下文= new Model();
var exp = new Collection< Expression< Func&Foo,object>>>();

Foo foo = new Foo();
Bar bar = new Bar();
员工emp = new Employee(){Name =employee};
银行银行=新银行(){名称=银行};
职位= new Position(){Name =position};
foo.Bars = new List< Bar>();
foo.Bars.Add(bar);
bar.Employees = new List< Employee>();
bar.Employees.Add(emp);
emp.Position = position;
emp.Bank = bank;
context.Foos.Add(foo);
context.SaveChanges();

exp.Add(f => f.Bars.Select(b => b.Employees.Select(e => e.Position)));
exp.Add(f => f.Bars.Select(b => b.Employees.Select(e => e.Bank)));

DbSet&Foo> dbSet = context.Set&Foo>();
IQueryable< Foo> query = dbSet;

if(exp!= null)
{
foreach(var incProp in exp)
{
query = query.Include(incProp);
}
}

var first = query.ToList()。FirstOrDefault();
var firstEmp = first.Bars.First()。Employees.First();
Console.WriteLine(String.Format({0} | {1} | {2},firstEmp.Name,firstEmp.Bank.Name,firstEmp.Position.Name));
}
}

}

输出: 员工|银行|位置



还有其他任何内容添加到查询中,或者您可能会以某种方式创建匿名类型? p>

I am passing values into a method which uses a foreach loop to iterate through a collection. In the loop, an Include statement is used from entity framework to eager load. This is what I pass in:

var exp = new Collection<Expression<Func<Foo,object>>>();

Why is it that when I use this:

exp.Add(f => f.Bars.Select(b=> b.Employees.Select( e=> e.Position)));
exp.Add(f => f.Bars.Select(b=> b.Employees.Select( e=> e.Bank)));

and Employee, Position, and Bank all have the field Name that it will jumble the Name between the different fields? As in, Bank and Position will both have Employee's name in their Name field. To be more explicit, for whatever reason

In DataBase:

Employee.Name = "Jon";
Employee.Bank.Name = "World Bank";
Employee.Position.Name = "CEO";

Data from .Include:

Employee.Bank.Name == "Jon" //true
Employee.Position.Name == "Jon" //true

Extra Information, inside of method that accepts exp

DbSet<Foo> dbSet = context.Set<Foo>();
IQueryable<Foo> query = dbSet;

if (exp != null)
{
 foreach (var incProp in exp)
 {
  query = query.Include(incProp);
 }
}

Am I doing something wrong in my code?

edit

public class Foo
{
 public int FooId { get; set; }
 public virtual List<Bar> Bars { get; set; }
}

public class Bar
{
 public int BarId { get; set; }
 public virtual Foo Foo { get; set; }
 public int FooId { get; set; }
 public virtual List<Employee> Employees { get; set; }
}

public class Employee
{
 public int EmployeeId { get; set; }
 public int BarId { get; set; }
 public virtual Bar Bar { get; set; }
 public int BankId { get; set; }
 public virtual Bank Bank { get; set; }
 public int PositionId { get; set; }
 public virtual Position Position { get; set; }
 public string Name { get; set; }
}

public class Bank
{
 public int BankId { get; set; }
 public string Name { get; set; }
}

public class Position
{
 public int PositionId { get; set; }
 public string Name { get; set; }
}

解决方案

I don't think the problem is in the code you're showing. I created a console app from what you've put above and it output what was in the database. Here's the entirety of the app:

namespace ExampleCF
{
    public class Foo
    {
        public int FooId { get; set; }
        public virtual List<Bar> Bars { get; set; }
    }

    public class Bar
    {
        public int BarId { get; set; }
        public virtual Foo Foo { get; set; }
        public int FooId { get; set; }
        public virtual List<Employee> Employees { get; set; }
    }

    public class Employee
    {
        public int EmployeeId { get; set; }
        public int BarId { get; set; }
        public virtual Bar Bar { get; set; }
        public int BankId { get; set; }
        public virtual Bank Bank { get; set; }
        public int PositionId { get; set; }
        public virtual Position Position { get; set; }
        public string Name { get; set; }
    }

    public class Bank
    {
        public int BankId { get; set; }
        public string Name { get; set; }
    }

    public class Position
    {
        public int PositionId { get; set; }
        public string Name { get; set; }
    }

    public class Model : DbContext
    {
        public DbSet<Foo> Foos { get; set; }
        public DbSet<Bar> Bars { get; set; }
        public DbSet<Employee> Employees { get; set; }
        public DbSet<Bank> Banks { get; set; }
        public DbSet<Position> Positions { get; set; }

        public Model()
        {
            Configuration.LazyLoadingEnabled = false;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Model context = new Model();
            var exp = new Collection<Expression<Func<Foo, object>>>();

            Foo foo = new Foo();
            Bar bar = new Bar();
            Employee emp = new Employee() { Name = "employee" };
            Bank bank = new Bank() { Name = "bank" };
            Position position = new Position() { Name = "position" };
            foo.Bars = new List<Bar>();
            foo.Bars.Add(bar);
            bar.Employees = new List<Employee>();
            bar.Employees.Add(emp);
            emp.Position = position;
            emp.Bank = bank;
            context.Foos.Add(foo);
            context.SaveChanges();

            exp.Add(f => f.Bars.Select(b => b.Employees.Select(e => e.Position)));
            exp.Add(f => f.Bars.Select(b => b.Employees.Select(e => e.Bank)));

            DbSet<Foo> dbSet = context.Set<Foo>();
            IQueryable<Foo> query = dbSet;

            if (exp != null)
            {
                foreach (var incProp in exp)
                {
                    query = query.Include(incProp);
                }
            }

            var first = query.ToList().FirstOrDefault();
            var firstEmp = first.Bars.First().Employees.First();
            Console.WriteLine(String.Format("{0} | {1} | {2}", firstEmp.Name, firstEmp.Bank.Name, firstEmp.Position.Name));
        }
    }

}

outputs: employee | bank |position

Is there anything else you're adding to the query, or maybe you're somehow creating an anonymous type?

这篇关于为什么这个Include不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆