每次创建新PurchasedProduct
并向其引用Product
时,在插入表格后,该表格始终会创建一个新的Product
并引用新的Purchase
而不是引用现有的。
..这意味着PurchasedProduct
可以包含多个Product
,每个public partial class fmAddEditPurchase : Form
{
List<Product> products;
Purchase purchase;
public fmAddEditPurchase()
{
InitializeComponent();
Purchase = new Purchase();
Text = "Add New Purchase";
dtpDate.Value = DateTime.Now.Date;
RefreshPurchasedProduct();
LoadProductList();
}
private void RefreshPurchasedProduct()
{
List<PurchasedProduct> ppQuery = new List<PurchasedProduct>();
BindingSource bi = new BindingSource();
if (Purchase.PurchasedProducts.Count > 0)
{
using (var context = new dbKrunchworkContext())
{
bi.DataSource = Purchase.PurchasedProducts.
Join(products, x => x.Product, y => y, (x, y) =>
new { y.Product_Name, x.Price, x.Quantity }).
ToList();
}
}
dgvPurchasedProduct.DataSource = bi;
dgvPurchasedProduct.Refresh();
}
private void LoadProductList()
{
using (var context = new dbKrunchworkContext())
{
products = context.Products.ToList();
}
cbProductName.DataSource = products.
Select(x => x.Product_Name).ToList();
}
private void btAddProduct_Click(object sender, EventArgs e)
{
decimal price = 0.0M;
if (decimal.TryParse(tbPrice.Text, out price) && price > 0)
{
PurchasedProduct temp = Purchase.PurchasedProducts.
FirstOrDefault(
x => x.Product ==
products[cbProductName.SelectedIndex] &&
x.Price == price);
if (temp == null)
{
PurchasedProduct newPP = new PurchasedProduct();
newPP.Product = products[cbProductName.SelectedIndex];
newPP.Purchase = Purchase;
newPP.Quantity = (int)numQuantity.Value;
newPP.Price = price;
if (newPP.Product != null)
{
Purchase.PurchasedProducts.Add(newPP);
}
}
else
{
temp.Quantity += (int)numQuantity.Value;
}
RefreshPurchasedProduct();
}
}
private void btSave_Click(object sender, EventArgs e)
{
try
{
Purchase.Received_Date = dtpDate.Value;
Purchase.Total_Amount = decimal.Parse(tbTotalPrice.Text);
Purchase.Note = tbNote.Text;
using (var context = new dbKrunchworkContext())
{
for (int i = 0; i < Purchase.PurchasedProducts.Count; i++)
{
PurchasedProduct pp =
Purchase.PurchasedProducts.ElementAt(i);
Product p = context.Products.
FirstOrDefault(x => x.ID == pp.Product.ID);
pp.Product = p;
}
}
}
catch (Exception)
{
}
}
}
代表购买的DialogResult() == DialogResult.OK
以及购买的数量(数量)。
以下是相关代码:
Form
这是从上面 private void Purchase_AddNewRecord()
{
fmAddEditPurchase addForm = new fmAddEditPurchase();
if (addForm.ShowDialog() ==
DialogResult.OK && addForm.Purchase.Total_Amount > 0)
{
using (var context = new dbKrunchworkContext())
{
context.Purchases.Add(addForm.Purchase);
context.SaveChanges();
}
}
}
收到Product
后将新记录插入表格的主要表单。
{{1}}
示例:
在
我做了什么(+保存)
之后(请注意,它会创建一个新的{{1}}而不是使用旧版本)
答案 0 :(得分:1)
您在表单的多种方法中使用DbContext
的多个实例。
您遇到此问题,因为以下代码会将所有对象都放入Purshase
图表中,并将所有对象标记为Added
州。
private void Purchase_AddNewRecord()
{
fmAddEditPurchase addForm = new fmAddEditPurchase();
if (addForm.ShowDialog() ==
DialogResult.OK && addForm.Purchase.Total_Amount > 0)
{
using (var context = new dbKrunchworkContext())
{
context.Purchases.Add(addForm.Purchase);
context.SaveChanges();
}
}
}
要解决此问题,您必须更改与Product
实例相关的每个Purshase
实例的状态,如下面的代码:
private void Purchase_AddNewRecord()
{
fmAddEditPurchase addForm = new fmAddEditPurchase();
if (addForm.ShowDialog() ==
DialogResult.OK && addForm.Purchase.Total_Amount > 0)
{
using (var context = new dbKrunchworkContext())
{
context.Purchases.Add(addForm.Purchase);
foreach (var purchasedProduct in addForm.Purchase.PurchasedProducts)
{
context.Entry(purchasedProduct.Product).State = EntityState.Unchanged;
}
context.SaveChanges();
}
}
}
当您使用Windows窗体或WPF时,建议您不要像实际操作那样为每个方法创建DbContext
的新实例。您必须通过为每个表单创建一个字段来创建一个表单。