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

  • Home
  • SEARCH
  • 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 7644503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:42:19+00:00 2026-05-31T09:42:19+00:00

I have to delete some rows in the database. I’m using Entity Framework 4.3

  • 0

I have to delete some rows in the database. I’m using Entity Framework 4.3 (code-first). I have a class that’s responsible for deleting the entities/rows. BUT, when I run my spec, it fails because nothing actually gets deleted. If I check the database, the items are still there… no work was done.

Here’s my test/spec:

public class when_removing_all_monkeys
{
    DefaultDataContext _databaseContext;
    IMonkeyRemover _monkeyRemover;

    protected override void Given()
    {
        Database.DefaultConnectionFactory = 
            new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");

        _databaseContext = new DefaultDataContext();
        _databaseContext.Database.Create();

        //Builder is from nBuilder, a tool that helps create 
        // populated objects and lists
        Builder<Monkey>.CreateListOfSize(10).Build().ToList()
            .ForEach(x => DatabaseContext.Monkies.Add(x));

        _monkeyRemover = new MonkeyRemover(_databaseContext);
    }

    protected override void When()
    {
        _monkeyRemover.RemoveAll();
    }

    [Test]
    public void it_should_remove_all_the_monkeys()
    {
        DatabaseContext.Monkies.Count().ShouldEqual(0);
    }

    protected override void Cleanup()
    {
        _databaseContext.Database.Delete();
        _databaseContext.Dispose();
    }
}

And here is my class:

public class MonkeyRemover : IMonkeyRemover
{
    readonly DefaultDataContext _databaseContext;

    public MonkeyRemover(DefaultDataContext databaseContext)
    {
        _databaseContext = databaseContext;
    }

    #region IMonkeyRemover Members

    public void RemoveAll()
    {
        _databaseContext.Monkies.ToList()
            .ForEach(x => _databaseContext.Monkies.Remove(x));            
        _databaseContext.SaveChanges();
    }

    #endregion
}

I tried running a SQL command to delete the items, but when the spec failed, I assumed it was because the DataContext was unaware of the changes made by the raw command. However, removing them through EF’s normal channels doesn’t seem to do the trick either.

The last time I had this problem, I ended up switching to NHibernate. This time I don’t have that option.

Thoughts?

EDIT:

Just in case I’ve done something silly in my data context class, here it is for your scrutiny:

public class DefaultDataContext : DbContext
{
    public DbSet<Monkey> Monkies { get; set; } 
}

SOLUTION:

Thanks to @Kiff for the answer. WOW… how simple. I had left out one very influential line. I did not SaveChanges() after creating the items in the Given() section. Here’s the updated spec:

public class when_removing_all_monkeys
{
    DefaultDataContext _databaseContext;
    IMonkeyRemover _monkeyRemover;

    protected override void Given()
    {
        Database.DefaultConnectionFactory = 
            new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");

        _databaseContext = new DefaultDataContext();
        _databaseContext.Database.Create();

        //Builder is from nBuilder, a tool that helps create 
        // populated objects and lists
        Builder<Monkey>.CreateListOfSize(10).Build().ToList()
            .ForEach(x => DatabaseContext.Monkies.Add(x));

         _databaseContext.SaveChanges(); //forgot to save changes 

        _monkeyRemover = new MonkeyRemover(_databaseContext);
    }

    protected override void When()
    {
        _monkeyRemover.RemoveAll();
    }

    [Test]
    public void it_should_remove_all_the_monkeys()
    {
        DatabaseContext.Monkies.Count().ShouldEqual(0);
    }

    protected override void Cleanup()
    {
        _databaseContext.Database.Delete();
        _databaseContext.Dispose();
    }
}
  • 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-31T09:42:20+00:00Added an answer on May 31, 2026 at 9:42 am

    Try calling _databaseContext.SaveChanges() in Given() right before initializing _monkeyRemover. The change tracker may be getting confused since the monkies were added (but not saved) and then removed.

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

Sidebar

Related Questions

I have to delete some rows from a data table. I've heard that it
I have a weird problem. i fetch some rows from database using: while($tag =
I have form, where some fields are looks like rows, so I can add/delete
I have some files that I'd like to delete the last newline if it
We have some methods that call File.Copy, File.Delete, File.Exists, etc. How can we test
I have a HAML page that lists some links to delete things that looks
I have a live database that had some data deleted from it and I
I have a PHP server script that SELECTs some data from a MySQL database.
I am facing a very peculiar problem that even when I delete some rows,
I have a piece of code that 'updates' an entry in the database by

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.