我试图通过Entity Framework 6 Code First使用MVC 5构建我的第一个应用程序但是我有一个问题,我的应用程序是在线餐馆,它包含Items类,它包含itemprice和itemamount,我想计算它的值itemprice *在Orders类中以总成本保存的itemamount,如何在初始化程序中执行此操作[在此处输入图像描述] [1] ???
var Items = new List<MenuItem>
{
new MenuItem { ItemName = "Cheese Piza", Itemdesrciption="Pizza with cheese", MenuID=int.Parse("3"), ItemPrice=Decimal.Parse("15.5"), ItemAmount=int.Parse("2")},
new MenuItem { ItemName = "Bolinez Pasta", Itemdesrciption="pasta with red sous", MenuID=int.Parse("3"),ItemPrice=Decimal.Parse("20.45"), ItemAmount=int.Parse("3")},
new MenuItem { ItemName = "Holy Cake", Itemdesrciption="Cake with choklet sous", MenuID=int.Parse("3"), ItemPrice=Decimal.Parse("45.5"), ItemAmount=int.Parse("1")},
};
Items.ForEach(I => context.Item.Add(I));
context.SaveChanges();
var Orderss = new List<Order>
{
new Order { CustomerID = int.Parse("1"), ItemID=int.Parse("3"), OrderDate= DateTime.Parse("2005-09-01"),
TotalPrice = },
};
答案 0 :(得分:0)
只需使用var price = Items.Sum(t => t.ItemPrice);
但是,我想你必须经常使用这段代码:)
Abaout int.parse read this
答案 1 :(得分:0)
您可以使用computed属性添加计算列。这应该创建一个在数据库中计算的价格列。私有集是为了使程序不能覆盖数据库提供的计算。
public class MenuItem
{
[Key]
public Int MenuID{ get; set; }
public string ItemName { get; set; }
public string Itemdesrciption { get; set; }
public Int ItemAmount{ get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public string ItemPrice
{
get { return /* do your sum here */ }
private set { /* needed for EF */ }
}
}
答案 2 :(得分:0)
我认为没有必要将数据保存在数据库中,因为它是一个计算字段,我想你可以向我们展示你的数据库的模型,观察你的逻辑,它就像主细节,老师在哪里可以是“订单”表,表格详细说明了“订单详细信息”订单的项目,还有一个表格列出了可用项目“项目”。
namespace Model
{
public class Order
{
public int Id { get; set; }
public int CustomerId { get; set; }
[ForeignKey( "CustomerId" )]
public virtual Customer Customer { get; set; }
public virtual List<OrderDetail> Details { get; private set; }
public double Total { get { return Details.Sum( t => t.SubTotal ); } }
public Order()
{
this.Details = new List<OrderDetail>();
}
}
public class OrderDetail
{
public int Id { get; set; }
public int OrderId { get; set; }
public virtual Order Order { get; set; }
public int ItemId { get; set; }
[ForeignKey( "ItemId" )]
public virtual Item Item { get; set; }
public int Quantity { get; set; }
public double SubTotal { get { return this.Item.Price * this.Quantity; } }
}
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public double Price { get; set; }
public double Cost { get; set; }
}
}
namespace Try
{
public class Program
{
public static void Main()
{
using (DataBaseContext context = new DataBaseContext())
{
context.Items.AddOrUpdate( t => t.Name,
new Model.Item() { Name = "MyProduct1", Price= 14.5, Cost=11.5, Description="" } ,
new Model.Item() { Name = "MyProduct2", Price = 16.5, Cost = 14.5, Description = "" },
new Model.Item() { Name = "MyProduct3", Price = 20.5, Cost = 18.5, Description = "" },
new Model.Item() { Name = "MyProduct4", Price = 10.5, Cost = 8.5, Description = "" }
);
context.Savechanges();
Model.Order Order= new Model.Order() { CustomerId=3};
Order.Details.AddRange( new Model.OrderDetail[] {
new Model.OrderDetail { ItemId= 1, Quantity=2},
new Model.OrderDetail { ItemId= 2, Quantity=1},
new Model.OrderDetail { ItemId= 3, Quantity=5},
new Model.OrderDetail { ItemId= 4, Quantity=6}
} );
Console.WriteLine( "Total: {0} for {1} Items", Order.Total, Order.Details.Count );
Console.ReadLine();
}
}
}
}
请启用Lazy以自动加载导航属性。