Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6252349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:47:04+00:00 2026-05-24T13:47:04+00:00

We use code-first EF 4 with DbContext and DbSet<> and POCOs with DataAnnotations. I

  • 0

We use code-first EF 4 with DbContext and DbSet<> and POCOs with DataAnnotations.
I am new to this and cannot seem to find an answer to my quesiton:

How can I delete multiple items from the DB directly without first selecting the items with LINQ and then doing loop and call Remove(item) on each iteration? That seems silly to me.

All references concerning this refer to function that don’t seem to exist, like DeleteOnSubmit(item) which isn’t there in my DbContext. Also, it only deletes ONE item.

Is there a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:47:05+00:00Added an answer on May 24, 2026 at 1:47 pm

    DeleteOnSubmit is function from DataContext class = Linq-to-SQL

    Yo don’t have to load item before you delete it. It is enough if you know its key. Something like this should work:

    var item = new Item { Id = someId };
    context.Items.Attach(item);
    context.Items.Remove(item);
    context.SaveChanges();
    

    or

    var item = new Item { Id = someId };
    context.Items.Attach(item);
    context.Entry(item).State = EntityState.Deleted;
    context.SaveChanges();
    

    There is no way to delete multiple items with EF (except cascade delete) without specifying each single item to be deleted. If you want to deleted multiple items directly you must use SQL like

    context.Database.ExecuteSqlCommand("DELETE ...");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use this code to determine checkbox width and height: var chk = $('input[type=checkbox]:first');
First the requirements: By management requirements, I can't use open source code. I need
I am trying to figure out how to use the new EF Code First
according to this page one can't use code like if variable = something(): #do
I'm attempting to use code I've found that uses Win32. However, I'm getting this
how does one use code to do this: produce 15 random numbers [EDIT: from
Please help me understand 2 things I found in this C code: First, there
I try to create database from entity framework code first follow with this tutorial
I'm currently working on a project which is using EF Code First with POCOs.
I'm currently working on a project which is using EF Code First with POCOs.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.