none
Entity Framework Insert or update data if exist RRS feed

  • Question

  • i am new in EF. i need to add data if it does not exist in db else update data.i got a code but could not understand how to call it. so need a small example which tell me how could i use AddOrModify to add or update my employee data.

    does the below code do the db round trip to check the data exist or not?

    how to make the below code extension method?

    public void AddOrModify<T>(T entity, string key) where T : class, IEntity // Implements MyKey 
    {
         using (var context = new MyContainer())
         {
             if (context.Set<T>().Any(e => e.MyKey == key))
             {
                  context.Entry(entity).State = EntityState.Modified;
             } 
             else
             {
                  context.Entry(entity).State = EntityState.Added;
             }
    
             context.SaveChanges();
         }
    }

    looking for help with sample code.

    Monday, October 9, 2017 7:43 PM

Answers

  • Hi Sudip_inn,

    I use WPF and EF 6.0 to make a data entry demo for your reference. Hope this can help you.

    When add or edit data, I use your code.

    Here is the snippet of the code.

    public void Save(object parameter)
            {
                if (parameter!=null && parameter is NewEmployeeWindow)
                {
                    NewEmployeeWindow editWindow = parameter as NewEmployeeWindow;
    
                    try
                    {
                        AddOrModify<Employee>(editWindow.SelectedEmployee, editWindow.SelectedEmployee.MyKey);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    editWindow.Close();
                }
                Reload();
    
            }
            public void Reload()
            {
                using (var db = new MyContainer())
                {
                    Employees = new ObservableCollection<Employee>(db.Employees.ToList());
                }
            }
    
            public void AddOrModify<T>(T entity, string key) where T : class, IEntity // Implements MyKey 
            {
                using (var context = new MyContainer())
                {
                    if (context.Set<T>().Any(e => e.MyKey == key))
                    {
                        context.Entry(entity).State = EntityState.Modified;   
                    }
                    else
                    {
                        context.Entry(entity).State = EntityState.Added;
                    }
    
                    context.SaveChanges();
                }
            }

    Check my project Here: https://1drv.ms/u/s!As1txuR5i35kj9MH4gbvSxUtg3DmWQ

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    • Edited by Bob Ding Wednesday, October 11, 2017 1:14 AM
    • Marked as answer by Sudip_inn Wednesday, November 1, 2017 8:52 AM
    Tuesday, October 10, 2017 10:58 AM