locked
save multiple selected rows to EF table RRS feed

  • Question

  • Hi All.

    How to create method to save multiple selected rows?

    Thanks.

    Wednesday, September 2, 2020 4:33 PM

All replies

  • If not using AsNoTracking SaveChanges will pickup the changes and update the table.

    For detached use UpdateRange

    public void UpdateMultipleRows(List<Customers> customers)
    {
        using (var context = new NorthwindContext())
        {
            context.Customers.UpdateRange(customers);
        }
    }
    
    For EF 6 updating detached entities you need to attach and set the state to modified then do SaveChanges.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, September 2, 2020 7:42 PM
  • Hi Karen. Thanks for reply.

    Sorry, that my first post has not enough detail description. Problem is:
    My WPF form has DataGrid and Save button. The first column of DataGrid is a CheckBox column. I'm populating the grid by this method

    Public static IEnumerable<EmployeeDepartmentDT> GetEmployeeList(int deptid)
    {
       using (var context = new Model1())
       {
           var emplist = 
               from employee in context.Employees
               where !context.EmployeeDepartment.Any(ed => (ed.EmpId == employee.ID) && (ed.DeptId == deptid))
               select new EmployeeDepartmentDT
               {
                  EmpName = employee.EmpName,
                  IsActive = employee.IsActive,
                  CreatedDate = employee.CreatedDate,
                  CreatedBy = employee.CreatedBy
               };
           return emplist.ToArray();
       }
    }

    I would like when the button is clicked, all row(s) where the CheckBoxe(s) is checked, insert into a EF table. How to do it? I will appreciate for sample.

    Thanks.


    • Edited by zleug Wednesday, September 2, 2020 9:16 PM
    Wednesday, September 2, 2020 8:18 PM
  • Hi zleug,

    Thank you for posting here.

    The question now is how to get the checked row of the checkbox in the datagrid, right?

    If so, please refer to the following code.

                List<People> peoples = new List<People>();
    
                for (int i = 0; i < TestDataGrid.Items.Count; i++)
                {
                    FrameworkElement item = TestDataGrid.Columns[0].GetCellContent(TestDataGrid.Items[i]);
                    DataGridTemplateColumn temp = (TestDataGrid.Columns[0] as DataGridTemplateColumn);
                    CheckBox cb = temp.CellTemplate.FindName("TestCheckBox", item) as CheckBox;
                    if (cb.IsChecked == true)
                    {
                        peoples.Add((People)TestDataGrid.Items[i]);
                    }
                }

    Then, please follow Karen's suggestion to write it to the database.

    Best Regards,

    Timon


    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.

    Thursday, September 3, 2020 5:49 AM
  • Hi,

    Has your issue been resolved?

    If so, please click on the "Mark as answer" option of the reply that solved your question, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    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.

    Wednesday, September 9, 2020 6:28 AM