【发布时间】:2019-06-19 13:01:02
【问题描述】:
我想加入并显示 tblOrder 、tblCustomer 、tblProduct 表中 OrderNo= givenId 的“ProductId、Quantity、UnitPrice、Discount & Total”列。
OrderNo 通过文本框给出。This is how it should be
订单表
public partial class tblOrder
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public tblOrder()
{
this.tblProducts = new HashSet<tblProduct>();
}
[Key]
public int OrderNo { get; set; }
public Nullable<int> Quantity { get; set; }
public Nullable<double> Discount { get; set; }
public Nullable<double> Total { get; set; }
public Nullable<double> SubTotal { get; set; }
public Nullable<double> DiscountTotal { get; set; }
public Nullable<double> Tax { get; set; }
public Nullable<double> NetTotal { get; set; }
public int CustomerCode { get; set; }
public virtual tblCustomer tblCustomer { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<tblProduct> tblProducts { get; set; }
}
产品表
public partial class tblProduct
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public tblProduct()
{
this.tblOrders = new HashSet<tblOrder>();
}
[Key]
public int ProductId { get; set; }
public string UnitPrice { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<tblOrder> tblOrders { get; set; }
}
客户表
public partial class tblCustomer
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public tblCustomer()
{
this.tblOrders = new HashSet<tblOrder>();
}
[Key]
public int CustomerCode { get; set; }
public string CustomerName { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<tblOrder> tblOrders { get; set; }
}
这是我创建的 ViewModel
public class OrderCustomerProductViewModel
{
public OrderCustomerProductViewModel() { }
public OrderCustomerProductViewModel(tblOrder orderow, tblCustomer customerow, tblProduct productrow)
{
OrderNo = orderow.OrderNo;
CustomerName = customerow.CustomerName;
ProductId = productrow.ProductId;
Quantity = orderow.Quantity;
UnitPrice = productrow.UnitPrice;
Discount = orderow.Discount;
Total = orderow.Total;
}
public int OrderNo { get; set; }
public string CustomerName { get; set; }
public int ProductId { get; set; }
public Nullable<int> Quantity { get; set; }
public string UnitPrice { get; set; }
public Nullable<double> Discount { get; set; }
public Nullable<double> Total { get; set; }
}
我尝试使用如下列表发送所有数据以查看,但遇到了一些错误。 它说
ICollection 不包含 ProductId 和 UnitPrice 的定义
请帮我解决这个错误。提前致谢
public ActionResult Index(int id)
{
var results = (db.tblOrders.Where(l => l.OrderNo == id).Include(c => c.tblCustomer).Include(p => p.tblProducts)
.Select(v => new OrderCustomerProductViewModel
{
CustomerName = v.tblCustomer.CustomerName,
ProductId = v.tblProducts.ProductId,
Quantity = v.Quantity,
UnitPrice = v.tblProducts.UnitPrice,
Discount = v.Discount,
Total = v.Total,
})).ToList();
return View(results);
}
! I tried as [Jaggan_j] said but it doesn't work. [Jaggan_j] Please help me3
【问题讨论】:
-
Product 和 Order 表是多对多的关系,您可以通过链接表将它们连接起来
-
我不能使用链接表来连接多对多表,因为实体框架消除了链接表。
标签: asp.net-mvc entity-framework many-to-many viewmodel jointable