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 created a C# class file by using a XSD-file as an input.
I have a simple Poco-Model using abstract classes, and it seems not to work
I have created domain model and define entities, value objects, Services and so on.
I'm starting out using the Zend Framework and have created a suitable model which
Using Microsoft's designer for the Entity Framework (v3.5), I have created an Entity Model
I have created a custom dialog for Visual Studio Setup Project using the steps
I have a simply Class that is intended to be a simple POCO -
Say I've got a domain model created from C# classes like this: public class
I have a model withs Parents, Children and Grandchildren, in a many-to-many relationship. Using
I have created a data model with in EF 4.0, however I am not

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.