为什么这不包括工作?

时间:2012-10-10 18:09:32

标签: c# mysql entity-framework-4 eager-loading

我将值传递给一个方法,该方法使用foreach循环来迭代集合。在循环中,从实体框架到急切加载使用Include语句。这就是我传递的内容:

var exp = new Collection<Expression<Func<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)));

和员工,职位和银行都有字段Name,它会在不同字段之间混淆名称?如同,Bank和Position将在其Name字段中都有Employee的名称。为了更明确,无论出于何种原因

在DataBase中:

Employee.Name = "Jon";
Employee.Bank.Name = "World Bank";
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);
 }
}

我在代码中做错了吗?

修改

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 个答案:

答案 0 :(得分:3)

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

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

}

输出:employee | bank |position

你还有什么要添加到查询中,或者你是否以某种方式创建了一个匿名类型?