troubleshooting Question

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

Avatar of LisaTatum
LisaTatum asked on
.NET ProgrammingDatabases
13 Comments1 Solution6040 ViewsLast Modified:
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);              
            }
        }
    }
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 13 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros