Moses Aprico February 2016

Cant delete table under one to many relationship

I have these 3 relevant tables below : enter image description here

I have a form. Say it is called fmEditPurchase. Inside that form, I can edit Purchase informations, and add / delete PurchasedProduct. enter image description here

There can be many changes in that form, but the changes will only be applied if I click on the save button, which returns DialogResult.OK to parent form. Below method is located in the main form.

    private void Purchase_EditPurchase(object sender, DataGridViewCellEventArgs e)
    {
        if (dgvPurchase.SelectedRows.Count == 1)
        {
            int index = dgvPurchase.SelectedCells[0].RowIndex;
            DataGridViewRow selectedRow = dgvPurchase.Rows[index];

            int id = (int)selectedRow.Cells["ID"].Value;

            Purchase edit = null;

            using (var context = new dbKrunchworkContext())
            {
                edit = context.Purchases.Where(x => x.ID == id).FirstOrDefault();

                if (edit != null)
                {
                    fmAddEditPurchase editForm = new fmAddEditPurchase(edit);

                    if (editForm.ShowDialog() == DialogResult.OK)
                    {
                        //Section 1
                        foreach (var item in editForm.DeletedPP)
                        {
                            context.Entry(item).State = EntityState.Deleted;
                            context.Entry(item.Product).State = EntityState.Unchanged;
                        }

                        //Section 2
                        context.Entry(editForm.Purchase).State = EntityState.Modified;

                        //Section 3
             

Answers


Rodolfo February 2016

According to your comment:

a purchase record can't have 2 PurchasedProduct record with a same product (just add the quantity instea)

You don't need to set the PK to Product_ID. FK should be enough. If you wanna increase the quantity just look for the record searching by Product_ID, update it and save. In that way deleting will be possible.


Moses Aprico February 2016

Finally solved it, while Rodolfo's answer contribute to the solution, but it's not enough.

These are what I did :

  1. I add my database context as parameter to the edit dialog constructor, and also a field inside that to store the context.

    private dbKrunchworkContext context;
    public fmAddEditPurchase(dbKrunchworkContext context)
    {
        //Bla bla
        this.context = context;
        //Bla bla
    }
    
  2. Before I initialize & show the dialog, I initialize the context using using(var context = new dbKrunchworkContext) { } and pass the value to the dialog.

    using (var context = new dbKrunchworkContext())
    {
        edit = context.Purchases.Where(x => x.ID == id).FirstOrDefault();
    
        if (edit != null)
        {
            fmAddEditPurchase editForm = new fmAddEditPurchase(context, edit);
    
            if (editForm.ShowDialog() == DialogResult.OK)
            {
                //Blabla
            }
        }
    }
    
  3. In the new dialog, every time I'm going to use a context, I use the context field instead of using a newly instatiated one.

    private void RefreshPurchasedProduct()
    {
        BindingSource bi = new BindingSource();
    
        //Bla bla
    
        bi.DataSource = Purchase.PurchasedProducts.
            Join(context.Products, x => x.Product.ID, y => y.ID, (x, y) =>
                new { y.Product_Name, x.Price, x.Quantity }).ToList();
    
        //Bla bla
    }
    

This way, there'll be no (annoying) The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.

Hope this helps anyone stumbled upon this similar problem of mine.

P.S. Some of the solution out there may prefer solve it with the static context, never tried it, but if me, I prefer this way, since it'll be cleaner this way, and less code to maintain.

Post Status

Asked in February 2016
Viewed 1,037 times
Voted 5
Answered 2 times

Search




Leave an answer