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 8114935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:18:23+00:00 2026-06-06T03:18:23+00:00

I tries to delete an entity – table This is the code: db.Sessions.SqlQuery(delete table

  • 0

I tries to delete an entity – table

This is the code:

db.Sessions.SqlQuery("delete table session");
db.SaveChanges();

This isnt delete the table. Or do anything..
db is inherits from DbContext object.

I’ve found a solution

foreach (var item in db.Sessions)
{
    db.Sessions.Remove(item);
}

db.SaveChanges();

But I’m sure theres a better way.

Thanks.

  • 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-06-06T03:18:25+00:00Added an answer on June 6, 2026 at 3:18 am

    SqlQuery as the name implies only for executing queries if you want to drop the table you need something else:

    With the use of the DbContext.Database property you can execute any DDL/DML query:

    db.DataBase.ExecuteSqlCommand("drop table sessions");
    

    I have also created a repro and it’s working fine with EF4.3.1 with SQL Server2008:

    using (var db = new EFRepros())
    {
        db.Sessions.Add(new Session() { Name = "Test1" });
        db.Sessions.Add(new Session() { Name = "Test2" });
        db.Sessions.Add(new Session() { Name = "Test3" });
        db.SaveChanges();
        Console.WriteLine(db.Sessions.Count()); // Output: 3
        db.Database.ExecuteSqlCommand("delete from sessions");
        //db.Database.ExecuteSqlCommand("truncate table sessions"); //this also works
        Console.WriteLine(db.Sessions.Count()); // Output: 0
    }
    

    Note: EF by default use plurar table names so you need to use sessions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my app crashes when the user tries to delete a row from the UITableView
This code is what i have tried to process the query, either delete or
Q1.Can LINQ to Entity perform create, update or delete operations on a table without
I tried to delete a document using db.users.remove({_id: 4f6f244f6f35438788aa138f}) but this commmand doesn't delete
For a few days Im being stuck at this problem, I've tried to delete/clean
How do I securely delete files in java? I tried the code at javafaq.nu
I need to create an entity with filtered results. eg. I have one table
I'm using Entity Frameworc 4.1 Code First and Moq . And I want to
I've tried using Entity Framework Code First and set AutomaticMigrationsEnabled to true so that
I have to delete some rows in the database. I'm using Entity Framework 4.3

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.