Select 在 EF6 中加载相关数据不起作用时包含在里面
Select inside Include When Loading Related Data in EF6 Is Not Working
我对 EF 6 有疑问。我的数据模型 class 中有以下语句,用于获取所有发票及其相关数据(PurchaseItems、Customer 和 CustomerAddress)。
public List<Invoice> GetAllInvoice()
{
using (var context = new InvoiceSolutionContext())
{
return context.Invoices.Include(p => p.PurchaseItems).Include(c => c.Customer.Select(ca => ca.CustomerAddress)).ToList();
}
}
上述方法在class.
中有如下using语句
使用 Invoicing.DomainModel;
使用 System.Collections.Generic;
使用 System.Data.Entity;
使用 System.Linq;
但问题是,Select(ca => ca.CustomerAddress) 无法正常工作,并出现以下编译错误。
'Customer' does not contain a definition for 'Select' and no accessible extension method 'Select' accepting a first argument of type 'Customer' could be found (are you missing a using directive or an assembly reference?)
以下是与上述问题相关的 class。
public class Invoice
{
[Key]
public int ID { get; set; }
[Required]
[Column("invoiceNumber")]
[Display(Name = "Invoice Number")]
public string InvoiceNumber { get; set; }
[Required]
[Column("description")]
public string Description { get; set; }
[Required]
public List<PurchaseItem> PurchaseItems { get; set; }
[Required]
[Column("totalCost")]
[Display(Name = "Total Cost")]
public decimal TotalCost { get; set; }
[Required]
[Column("taxTotal")]
[Display(Name = "Tax Amount")]
public decimal TaxAmount { get; set; }
[Column("otherCost")]
[Display(Name = "Other Cost")]
public decimal OtherCost { get; set; }
[Required]
[Column("subTotal")]
[Display(Name = "Sub Total")]
public decimal SubTotal { get; set; }
#region Foreign Key Ref
[Column("customerID")]
public int CustomerID { get; set; }
public Customer Customer { get; set; }
[Column("accountID")]
public int AccountID { get; set; }
public Account Account { get; set; }
#endregion
[Column("dateCreated")]
public DateTime DateCreated { get; set; }
[Column("dateModified")]
public DateTime DateModified { get; set; }
[Column("status")]
public bool Status { get; set; }
}
public class PurchaseItem
{
[Key]
public int ID { get; set; }
[Required]
[Column("description")]
public string Description { get; set; }
[Required]
[Column("unitPrice")]
[Display(Name = "Unit Price")]
public decimal UnitPrice { get; set; }
[Required]
[Column("quantity")]
public int Quantity { get; set; }
[Required]
[Column("amount")]
public decimal Amount { get; set; }
//foreign key reference
[Column("invoiceID")]
public int InvoiceID { get; set; }
public Invoice Invoice { get; set; }
}
public class Customer
{
[Key]
public int ID { get; set; }
[Required]
[Column("firstName")]
[MaxLength(250, ErrorMessage ="First name must have 250 characters or less")]
[Display(Name ="First Name")]
public string FirstName { get; set; }
[Required]
[Column("lastName")]
[MaxLength(250, ErrorMessage ="Last name must have 250 characters or less")]
[Display(Name ="Last Name")]
public string LastName { get; set; }
[Required]
[Column("emailAddress")]
[MaxLength(255, ErrorMessage ="Email address must have 255 characters or less")]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email Address")]
public string EmailAddress { get; set; }
[Required]
[Column("contactNumber")]
[Display(Name ="Contact Number")]
public string ContactNumber { get; set; }
[Column("abn")]
public string ABN { get; set; }
[Column("dateCreated")]
public DateTime DateCreated { get; set; }
[Column("dateModified")]
public DateTime DateModified { get; set; }
[Column("status")]
public bool Status { get; set; }
public List<Invoice> Invoices { get; set; }
public CustomerAddress CustomerAddress { get; set; }
}
public class CustomerAddress
{
[Key, ForeignKey("Customer")]
public int ID { get; set; }
[Required]
[Column("buildingNumber")]
[MaxLength(255, ErrorMessage ="Building number must have 255 characters or less")]
[Display(Name ="Building Number")]
public string BuildingNumber { get; set; }
[Required]
[Column("streetName")]
[MaxLength(255, ErrorMessage ="Street name must have 255 characters or less")]
[Display(Name ="Street Name")]
public string StreetName { get; set; }
[Required]
[Column("suburb")]
[MaxLength(255, ErrorMessage ="Suburb must have 255 characters or less")]
public string Suburb { get; set; }
[Required]
[Column("state")]
public string State { get; set; }
[Column("postCode")]
[Display(Name ="Postcode")]
public int PostCode { get; set; }
public Customer Customer { get; set; }
}
谁能告诉我这里的问题是什么?感谢您对此的帮助
编辑:下面是 class 图
Select
是集合的 Linq 表达式。客户是一个单一的实体。您需要对多个级别使用 Include
来预先加载它们:
return context.Invoices
.Include(p => p.PurchaseItems)
.Include(c => c.Customer.CustomerAddress)
.ToList();
当面对集合下方的引用时,您可以按名称预先加载它们。例如,如果 PurchaseItems 有一个产品:
return context.Invoices
.Include(p => p.PurchaseItems)
.Include("PurchaseItems.Product") // Loads product under each Purchase Item
.Include(c => c.Customer.CustomerAddress)
.ToList();
我相信这也可以使用 Select
:
来完成
.Include(p => p.PurchaseItems.Select(pi => pi.Product))
它适用于 PurchaseItems,因为它是一个集合,而 Customer 是一个单独的引用。
我对 EF 6 有疑问。我的数据模型 class 中有以下语句,用于获取所有发票及其相关数据(PurchaseItems、Customer 和 CustomerAddress)。
public List<Invoice> GetAllInvoice()
{
using (var context = new InvoiceSolutionContext())
{
return context.Invoices.Include(p => p.PurchaseItems).Include(c => c.Customer.Select(ca => ca.CustomerAddress)).ToList();
}
}
上述方法在class.
中有如下using语句
使用 Invoicing.DomainModel;
使用 System.Collections.Generic;
使用 System.Data.Entity;
使用 System.Linq;
但问题是,Select(ca => ca.CustomerAddress) 无法正常工作,并出现以下编译错误。
'Customer' does not contain a definition for 'Select' and no accessible extension method 'Select' accepting a first argument of type 'Customer' could be found (are you missing a using directive or an assembly reference?)
以下是与上述问题相关的 class。
public class Invoice
{
[Key]
public int ID { get; set; }
[Required]
[Column("invoiceNumber")]
[Display(Name = "Invoice Number")]
public string InvoiceNumber { get; set; }
[Required]
[Column("description")]
public string Description { get; set; }
[Required]
public List<PurchaseItem> PurchaseItems { get; set; }
[Required]
[Column("totalCost")]
[Display(Name = "Total Cost")]
public decimal TotalCost { get; set; }
[Required]
[Column("taxTotal")]
[Display(Name = "Tax Amount")]
public decimal TaxAmount { get; set; }
[Column("otherCost")]
[Display(Name = "Other Cost")]
public decimal OtherCost { get; set; }
[Required]
[Column("subTotal")]
[Display(Name = "Sub Total")]
public decimal SubTotal { get; set; }
#region Foreign Key Ref
[Column("customerID")]
public int CustomerID { get; set; }
public Customer Customer { get; set; }
[Column("accountID")]
public int AccountID { get; set; }
public Account Account { get; set; }
#endregion
[Column("dateCreated")]
public DateTime DateCreated { get; set; }
[Column("dateModified")]
public DateTime DateModified { get; set; }
[Column("status")]
public bool Status { get; set; }
}
public class PurchaseItem
{
[Key]
public int ID { get; set; }
[Required]
[Column("description")]
public string Description { get; set; }
[Required]
[Column("unitPrice")]
[Display(Name = "Unit Price")]
public decimal UnitPrice { get; set; }
[Required]
[Column("quantity")]
public int Quantity { get; set; }
[Required]
[Column("amount")]
public decimal Amount { get; set; }
//foreign key reference
[Column("invoiceID")]
public int InvoiceID { get; set; }
public Invoice Invoice { get; set; }
}
public class Customer
{
[Key]
public int ID { get; set; }
[Required]
[Column("firstName")]
[MaxLength(250, ErrorMessage ="First name must have 250 characters or less")]
[Display(Name ="First Name")]
public string FirstName { get; set; }
[Required]
[Column("lastName")]
[MaxLength(250, ErrorMessage ="Last name must have 250 characters or less")]
[Display(Name ="Last Name")]
public string LastName { get; set; }
[Required]
[Column("emailAddress")]
[MaxLength(255, ErrorMessage ="Email address must have 255 characters or less")]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email Address")]
public string EmailAddress { get; set; }
[Required]
[Column("contactNumber")]
[Display(Name ="Contact Number")]
public string ContactNumber { get; set; }
[Column("abn")]
public string ABN { get; set; }
[Column("dateCreated")]
public DateTime DateCreated { get; set; }
[Column("dateModified")]
public DateTime DateModified { get; set; }
[Column("status")]
public bool Status { get; set; }
public List<Invoice> Invoices { get; set; }
public CustomerAddress CustomerAddress { get; set; }
}
public class CustomerAddress
{
[Key, ForeignKey("Customer")]
public int ID { get; set; }
[Required]
[Column("buildingNumber")]
[MaxLength(255, ErrorMessage ="Building number must have 255 characters or less")]
[Display(Name ="Building Number")]
public string BuildingNumber { get; set; }
[Required]
[Column("streetName")]
[MaxLength(255, ErrorMessage ="Street name must have 255 characters or less")]
[Display(Name ="Street Name")]
public string StreetName { get; set; }
[Required]
[Column("suburb")]
[MaxLength(255, ErrorMessage ="Suburb must have 255 characters or less")]
public string Suburb { get; set; }
[Required]
[Column("state")]
public string State { get; set; }
[Column("postCode")]
[Display(Name ="Postcode")]
public int PostCode { get; set; }
public Customer Customer { get; set; }
}
谁能告诉我这里的问题是什么?感谢您对此的帮助
编辑:下面是 class 图
Select
是集合的 Linq 表达式。客户是一个单一的实体。您需要对多个级别使用 Include
来预先加载它们:
return context.Invoices
.Include(p => p.PurchaseItems)
.Include(c => c.Customer.CustomerAddress)
.ToList();
当面对集合下方的引用时,您可以按名称预先加载它们。例如,如果 PurchaseItems 有一个产品:
return context.Invoices
.Include(p => p.PurchaseItems)
.Include("PurchaseItems.Product") // Loads product under each Purchase Item
.Include(c => c.Customer.CustomerAddress)
.ToList();
我相信这也可以使用 Select
:
.Include(p => p.PurchaseItems.Select(pi => pi.Product))
它适用于 PurchaseItems,因为它是一个集合,而 Customer 是一个单独的引用。