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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:50:09+00:00 2026-05-20T10:50:09+00:00

I have created a model POCO class called Recipe ; a corresponding RecipeRepository persists

  • 0

I have created a model POCO class called Recipe; a corresponding RecipeRepository persists these objects. I am using Code First on top of an existing database.

Every Recipe contains an ICollection<RecipeCategory> of categories that link the Recipes and the Categories table in a many-to-many relationship. RecipeCategory contains the corresponding two foreign keys.

A simplified version of my controller and repository logic looks like this (I have commented out all checks for authorization, null objects etc. for simplicity):

public ActionResult Delete(int id)
{
    _recipeRepository.Remove(id);

    return View("Deleted");
}

The repository’s Remove method does nothing but the following:

public void Remove(int id)
{
    Recipe recipe = _context.Recipes.Find(id);
    _context.Recipes.Remove(recipe);
    _context.SaveChanges();
}

Howevery, the code above does not work since I receive a System.InvalidOperationException every time I run it: Adding a relationship with an entity which is in the Deleted state is not allowed.

What does the error message stand for and how can I solve the problem? The only thing I try to achieve is deleting an entity.


@Ladislav: I have replaced ICollection<RecipeCategory> by ICollection<Category>. Accidentially, ReSharper refactored away the virtual keyword.

However, the problem remains — I cannot delete a Category from a Recipe entity. The following code does not persist the deletion of the categories to the database:

private void RemoveAllCategoriesAssignedToRecipe()
{
    foreach (Category category in _recipe.Categories.ToArray())
    {
        _recipe.Categories.Remove(category);
        category.Recipes.Remove(_recipe);
    }

    _context.SaveChanges();
}

I have debugged the code and can confirm that the collections are modified correctly — that is, they contain no elements after the loop (I have also used the Clear() method). After calling SaveChanges(), they are populated again.

What am I doing wrong?

(Maybe it is important: I am using the Singleton pattern to only have one instance of the context.)

  • 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-20T10:50:10+00:00Added an answer on May 20, 2026 at 10:50 am

    I was able to solve the problem the following way:

    private void RemoveAllCategoriesAssignedToRecipe()
    {
        foreach (Category category in _recipe.Categories.ToArray())
        {
            Category categoryEntity = _categoryRepository.Retrieve(category.CategoryID);
    
            var recipesAssignedToCategory = categoryEntity.Recipes.ToArray();
            categoryEntity.Recipes.Clear();
    
            foreach (Recipe assignedRecipe in recipesAssignedToCategory)
            {
                if (assignedRecipe.RecipeID == _recipe.RecipeID)
                {
                    continue;
                }
    
                categoryEntity.Recipes.Add(assignedRecipe);
            }
    
            _context.Entry(categoryEntity).State = EntityState.Modified;
        }
    
        _recipe.Categories.Clear();
        _context.SaveChanges();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly large EF4 model, using POCO code gen. I've got lots
I'm using EF4.1 in code-first style to persist POCO objects for a small ASP.NET
Hey, sorry for my bad english... Using EF4 code-only, I have created some POCO
I have created one Catalyst application and I have created Schema and Model using
I have a Model created with a table called Customers. It contains the following
I have created domain model and define entities, value objects, Services and so on.
Ok... So I have created my model using EF4. Great! I then turned off
I have model created with visual designer. Now I want to have POCO classes
I am using PHP Yii Framework with MongoDB(yiimongodbsuite). I have created a Model which
I have the following POCO Class with its Repository Pattern Implementation. If my model

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.