【发布时间】:2020-03-03 03:13:00
【问题描述】:
我有一个关于 EF 6 的问题。我的数据模型类中有以下语句,用于获取所有发票及其相关数据(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();
}
}
上述方法在类中有以下 using 语句。
使用 Invoicing.DomainModel;
使用 System.Collections.Generic;
使用 System.Data.Entity;
使用 System.Linq;
但问题是,Select(ca => ca.CustomerAddress) 不起作用并给我以下编译错误。
“客户”不包含“选择”的定义,并且找不到接受“客户”类型的第一个参数的可访问扩展方法“选择”(您是否缺少 using 指令或程序集引用?)
以下是我与上述问题相关的课程。
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; }
}
谁能告诉我这里的问题是什么?感谢您对此的帮助
【问题讨论】:
标签: entity-framework entity-framework-6