How to delete multiple rows of data using linq in EF using DbContext

As part of the project, I have a database table with the following columns

enter image description here

I would like to be able to remove from this table all rows that have the corresponding SharingAgencyId and ReceivingAgencyId values ​​that I can pass.

What I have tried so far:

 public static ICollection<SecurityDataShare> UpdateSharedEntites(long conAgency, long recAgency)
        {
            ICollection<SecurityDataShare> agShares = null;
            try
            {
                using (var context = new ProjSecurityEntities(string.Empty))
                {
                    agShares = (from a in context.SecurityDataShares
                               .Where(c => c.ReceivingAgencyId == recAgency && c.SharingAgencyId == conAgency)
                                select a); 
                }
            }
            catch (Exception ex)
            {
                //ToDo
                throw;
            }
        }

My thought process was to get records in which the identifier matched the parameters passed, and then using the foreach iteration loop through (agShare) and deleting each line, and then saving my changes. In the current implementation, I do not seem to have access to any of the Delete methods.

Looking at the example above, I would appreciate any suggestions on how to remove rows in a table containing values ​​43 and 39 using dbContext.

+3
3

, DbContext, SecurityDataShares, IDbSet<SecurityDataShare>. , Remove.

foreach(var agShare in agShares) {
    context.SecurityDataShares.Remove(agShare);
}
context.SaveChanges();

, SQL . , , .

+5

Entity Framework ( ). , SQL / SQL- dbcontext.

+2

you can also transfer data to a stored procedure with a database containing a table of the dynamic type of your type and use this table in the stored procedure to delete matching rows from the database table.

0
source

All Articles