如何为项列表制作EF6 Include()子类导航属性

时间:2016-01-21 19:40:42

标签: entity-framework-6

我正在尝试选择发票及其子类列表。 以下是模型:

    public class SalesContext : DbContext {
        public DbSet<Product> Products { get; set; }
        public DbSet<Invoice> Invoices { get; set; }
        public DbSet<InvoiceItem> InvoiceItems { get; set; }

        public SalesContext() : base ("DefaultConnection") {

        }
    }

    public class Invoice {
        public int Id { get; set; }
        public List<InvoiceItem> Items { get; set; }

        public Invoice() {
            Items = new List<InvoiceItem>();
        }
    }

    public abstract class InvoiceItem {
        public int Id { get; set; }
        public int Qty { get; set; }
    }

    public class ProductInvoiceItem : InvoiceItem {
        public int ProductId { get; set; }
        public Product Product { get; set; }
    }

    public class Product {
        public int Id { get; set; }
        public string Name { get; set; }
    }

以下是种子数据:

    internal sealed class Configuration : DbMigrationsConfiguration<SalesContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
        }

        protected override void Seed(SalesContext context) {
            context.Products.AddOrUpdate(p => p.Name,
                new Product {Name = "Bible - New Living Translation"},
                new Product {Name = "Bible - King James Version"}
            );
            context.SaveChanges();
            context.Invoices.AddOrUpdate(new Invoice {
                    Items =
                        new List<InvoiceItem>() {
                            new ProductInvoiceItem {ProductId = 1},
                            new ProductInvoiceItem {ProductId = 2}
                        }
                }
                , new Invoice {
                    Items =
                        new List<InvoiceItem>() {
                            new ProductInvoiceItem {ProductId = 1}
                        }
                }
                );
            context.SaveChanges();
        }
    }

在这里,我试图用其关联的子类属性选择数据。

    public class SalesController : Controller
    {
        private SalesContext db = new SalesContext();

        // GET: Sales
        public ActionResult Index() {
            var query = db.Invoices.Include(i => i.Items);
            var query2 = query.Include(i => i.Items.OfType<ProductInvoiceItem>().Select(pi => pi.Product));
            var list = query2.ToList();
            return View(list);
        }
    }

在上面的代码示例中,query2被破坏了。它会抛出运行时异常。

  

System.ArgumentException未被用户代码
处理   HResult = -2147024809 Message =必须引用Include路径表达式   到类型上定义的导航属性。使用虚线路径   引用导航属性和集合的Select运算符   导航属性。参数名称:path ParamName = path
  Source = EntityFramework StackTrace:          在System.Data.Entity.QueryableExtensions.Include [T,TProperty](IQueryable 1 source, Expression 1路径)          在ParentChildEntityFrameworkStuff.Web.Controllers.SalesController.Index()   在c:\ users \ brandon.miller \ documents \ visual studio中   2015年\项目\ ParentChildEntityFrameworkStuff \ ParentChildEntityFrameworkStuff.Web \ \控制器SalesController.cs:行   20          在lambda_method(Closure,ControllerBase,Object [])          在System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase控制器,Object []参数)          在System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext   controllerContext,IDictionary 2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary 2   参数)          在System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()          在System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult)   asyncResult,ActionInvocation innerInvokeState)          在System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult 2.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase 1.End()          在System.Web.Mvc.Async.AsyncResultWrapper.End [TResult](IAsyncResult)   asyncResult,Object标签)          在System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult)   asyncResult)          在System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3d()          在System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters。&lt;&gt; c__DisplayClass46.b__3f()   的InnerException:

如果我使用查询并删除query2的东西,它会返回数据,但当然会在ProductInvoiceItems上省略Product。 ProductId字段已设置,因此我知道它至少获得了ProductInvoiceItems的值类型。我尝试过并尝试过搜索和搜索,并且无法为此找到解决方案,可以说是常见的用例。

如果急切地加载产品导航属性,我该怎么办?

0 个答案:

没有答案