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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:20:36+00:00 2026-06-17T23:20:36+00:00

How I can delete an entity using EF5? I’m getting this error: The object

  • 0

How I can delete an entity using EF5? I’m getting this error:

The object cannot be deleted because it was not found in the ObjectStateManager.

when I try to call .Remove on my DbSet. After googling I tried that

mycontext.Attach(entity)
mycontext.Remove(entity)

But in this way I get:

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

So, is it or not in ObjectStateManager?! 🙂

My entity is like that:

[Table("Words")]
public class Word : IWord
{
    [Key, Required]
    public int WordId { get; set; }

    [Required, StringLength(50)]
    public string Tag { get; set; }

    //Foreign Key
    public int VocabularyId { get; set; }

    //Navigation
    public virtual Vocabulary Vocabulary { get; set; }
    public virtual Language Language { get; set; }
    public virtual List<Translation> Translations { get; set; }

}
  • 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-17T23:20:38+00:00Added an answer on June 17, 2026 at 11:20 pm

    You can consider 2 scenario:

    1. Connected scenario: you load the entity from database and mark it as deleted

    var word = ctx.Words.Where(a=>a.WordId == wordId).First();
    ctx.DeleteObject(word);
    ctx.SaveChanges();
    

    2. Disconnected scenario: attach an existing entity and mark it as deleted

    var word = new Word() { WordId = id };
    ctx.Words.Attach(word);
    ctx.DeleteObject(word);
    ctx.SaveChanges();
    

    The second approach will help you avoid un-necessary database round trip

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

Sidebar

Related Questions

How I can delete an object which I had added before with this code.
I can delete an entity using the Deleting The entity given by Google Document,
When calling AcceptChanges I get the following error: AcceptChanges cannot continue because the object's
How can I delete all the listed files using PHP? As shown in the
How can i delete a html-special char \ from a string. $string = 'This
I want to delete content of all tables (all entities) using Entity Framework 4+.
I am wondering how can one delete an entity having just its ID and
I deleted a table in management studio, using code first entity framework shouldn't it
I am getting a '401 - Not Authorized' error message returned when I attempt
I am using Entity Framework v4 and the repository pattern found in the answer

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.