Why doesn't this Include work?

2019-07-22 11:37发布

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

1条回答
forever°为你锁心
2楼-- · 2019-07-22 12:19

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?

查看更多
登录 后发表回答