2012-05-09 35 views
0
foreach (StockItem item in StockList) 
{ 
    Master master = new Master(); 
    master.VoucherNo = BillNo; 
    master.Voucher = "Sales"; 
    master.StockName = StockList[0].StockName; 
    master.Quantity = StockList[0].Quantity; 
    master.Unit = StockList[0].Unit; 
    master.Price = StockList[0].UnitPrice; 
    master.Amount = StockList[0].Amount; 
    dbContext.AddToMasters(master); 
    dbContext.SaveChanges(); 
} 
Sale sale = new Sale(); 
sale.InvoiceNo = BillNo; 
sale.Date = BillDate; 
sale.Party = Customer; 
sale.Amount = (decimal)TotalAmount; 
dbContext.AddToSales(sale); 
dbContext.SaveChanges(); 

如果有n行,此代码只会从StockList开始全部n次添加第一行。实体框架从列表中向数据库添加第一行?

代码出了什么问题?

回答

2

你迭代StockList,但你实际上并没有使用迭代变量。

无处不在,您使用StockList [0],您应该使用项目。

编辑:这是你的循环应该是什么样子:

foreach (StockItem item in StockList) 
{ 
    Master master = new Master(); 
    master.VoucherNo = BillNo; 
    master.Voucher = "Sales"; 
    master.StockName = item.StockName; 
    master.Quantity = item.Quantity; 
    master.Unit = item.Unit; 
    master.Price = item.UnitPrice; 
    master.Amount = item.Amount; 
    dbContext.AddToMasters(master); 
    dbContext.SaveChanges(); 
} 
+0

不明白。 –

+0

好的,我明白了。谢谢.... –

相关问题