Monday, October 31, 2011

Entity is currently being edited and has uncommitted changes - EndEdit before changes can be... (Exception)


I was facing the same problem as many people has faced this problem while I searched the internet for solution,  my scenario is that I am inserting data from a child form, then it updated in datagrid and as well inserted in database it is working fine. but after adding if user delete the new added entity it give me above exception and delete failed. But if you sort or search the grid after adding or refresh the page it works fine user can just can select the new added entity and delete perfectly.
Here is my code
this is code for adding the input from childform on closing event of child form It will add the data into database and update the datagrid.
private void NewCompany_Closed(object sender, EventArgs e)
        {
            var companyView = (AddCompanyView)sender;
            if (companyView.NewCompany.OrganizationNumber != null)
            {   
                companyDomainDataSource.DataView.Add(companyView.NewCompany);
                CompanyDataGrid.CommitEdit(DataGridEditingUnit.Row, true );
                CompanyDataGrid.CancelEdit();
              
                companyDomainDataSource.SubmitChanges();
            }
            companyView.NewCompanyForm.CancelEdit();
       
        }

This is my delete method.

private void DeleteCompany_Click(object sender, RoutedEventArgs e)
        {

            MessageBoxResult result = MessageBox.Show("Are you sure you want to delete the selected company?", "Message",
                                                      MessageBoxButton.OKCancel);

            if(result == MessageBoxResult.OK)
            {
                var selectedCompany = (Company)CompanyDataGrid.SelectedItem;

                var counts = selectedCompany.Fleet.Count;

                if (counts != 0)
                {

                    MessageBox.Show("You cannot delete selected company because there are fleets registered under this company!", "Message",
                                                             MessageBoxButton.OK);
                }
                else
                {
                    try
                    {
                        var dctx = (TNTDS)companyDomainDataSource.DomainContext;

                        if (result == MessageBoxResult.OK)
                        {
                            dctx.Companies.Remove(selectedCompany);
                            dctx.SubmitChanges();
                            MessageBox.Show("Deleted Successfully!", "Message", MessageBoxButton.OK);
                        }

                    }
                    catch (Exception ex)
                    {

                        MessageBox.Show(ex.ToString(), "Message", MessageBoxButton.OK);
                    }
                }
            }

        }


Here is solution I found. Its very simple just send changes from different contexts so it will avoid any confusion to Entity framework
For example for inserting data use 
companyDomainDataSource.DomainContext.SubmitChanges();
And for deleting
companyDomainDataSource.SubmitChanges();
It will work perfectly and datagrid will show changes according while inserting and deleting both.
Thanks!

No comments:

Post a Comment