We help IT Professionals succeed at work.
Get Started

LINQ to Entities: UpdateException - after it occurs whole database seems to be screwed

LisaTatum
LisaTatum asked
on
6,036 Views
Last Modified: 2013-12-17
Hello,

this is my first question here. If I could I would give this question 1000 points, because an answer is  very important to me. I already asked on microsoft forum, but nobody did answer... If someone has a helping answer in finding a solution I decide to suscribe EE - not kidding -

My problem is a easy test scenario:

I have just ONE single Entity called DEPARTMENT.
It has ONE field called department_name which is set as primary key in the database.

When I add a department name to the database which already exists I get an UpdateException.

Error:

at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
at System.Data.Objects.ObjectContext.SaveChanges(Boolean acceptChangesDuringSave) at System.Data.Objects.ObjectContext.SaveChanges()
at Company.DepartmentPresenter.View_AddDepartment(String departmentName) in C:\MVP.EF\DepartmentPresenter.cs:line 43.
A first chance exception of type 'System.Data.UpdateException' occurred in System.Data.Entity.dll

After I got that UpdateException I tried to add other departments names not yet saved in the database. But whatever name I add now I get always an UpdateException independendly what department name I  have added to the database.
Or I delete department names from the ListBox and they get deleted in the ListBox but not in the database and everytime I try to delete a department an UpdateException is thrown.


Why does Entity Framework tell me about a Primary Key violation AGAIN although I enter a value not saved yet in the database ?

There must be something breaking my database behaviour...


VIEW:
 
public partial class MainWindowView : Form , IDepartmentView
    {       
        private IDepartmentPresenter departmentPresenter;            
        public event AddViewDepartmentDataHandler AddViewDepartmentData;
        public event DeleteViewDepartmentDataHandler DeleteViewDepartmentData;
        public event DisposeContextHandler DisposeContext;
 
        public MainWindowView()
        {
            InitializeComponent();
            departmentPresenter = new DepartmentPresenter(this);
            
        }
 
        public void DataToBindingSource(Object queryDepartment)
        {
            dEPARTMENTBindingSource.DataSource = queryDepartment;             
        }  
 
        private void addDepartmentButton_Click_1(object sender, EventArgs e)
        {
            if (AddViewDepartmentData != null)
                AddViewDepartmentData(departmentTextBox.Text);            
        }
 
        private void deleteDepartmentButton_Click(object sender, EventArgs e)
        {
            if (DeleteViewDepartmentData != null)
            {
                DEPARTMENT department = (DEPARTMENT)departmentListBox.SelectedItem;
 
                if (department == null)
                {
                    MessageBox.Show("Sie müssen ein existierendes Department wählen", "Fehler");
                    return;
                }                 
 
                DeleteViewDepartmentData(department);
            }                
        }                        
    }
 
PRESENTER:
 
 class DepartmentPresenter : IDepartmentPresenter
    {
        private companyEntities mycontext = new companyEntities();
        private IDepartmentView view;
        private ObjectQuery<DEPARTMENT> queryDepartment;           
 
        public DepartmentPresenter(IDepartmentView view)
       {           
           this.view = view;       
           LoadDepartments();
           view.AddViewDepartmentData    += View_AddDepartment;  
           view.DeleteViewDepartmentData += View_DeleteDepartment;           
       }
 
        private void LoadDepartments()
        {
            try
            {
                queryDepartment = mycontext.DEPARTMENT;
                this.view.DataToBindingSource(queryDepartment);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.StackTrace);
            }
        }
 
        private void View_AddDepartment(String departmentName)
        {
            var department = new DEPARTMENT();
 
            try
            {
                department.department_name = departmentName;
                mycontext.AddToDEPARTMENT(department);
                mycontext.SaveChanges();
                
                this.view.DataToBindingSource(queryDepartment.Execute(MergeOption.AppendOnly));                
            }
            catch (UpdateException ex)
            {
                Console.WriteLine("Error: " + ex.StackTrace);
                MessageBox.Show("The department: " + departmentName + " already exists", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }            
        }
 
        private void View_DeleteDepartment(DEPARTMENT department)
        {
            try
            {
                mycontext.DeleteObject(department);
                mycontext.SaveChanges();
            }
            catch (UpdateException ex)
            {
                Console.WriteLine("Error: " + ex.StackTrace);              
            }
        }
    }

Open in new window

Comment
Watch Question
Retired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
This problem has been solved!
Unlock 1 Answer and 13 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE