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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:11:47+00:00 2026-05-31T15:11:47+00:00

This is the code that I have in my controller: [HttpPost] public ActionResult UpdateArticle(Article

  • 0

This is the code that I have in my controller:

   [HttpPost]
   public ActionResult UpdateArticle(Article article)
   {
       if (ModelState.IsValid)
       {
           article.DateAdded = 
               this.articleRepository.GetSingle(article.ID).DateAdded;
           article.DateModified = DateTime.Now;

           this.articleRepository.Update(article);
               return this.Redirect("~/Admin");
       }
       else
       {
           return this.Redirect("~/Admin/UnsuccessfulOperation");
       }
   }

From the view the data comes updated. I have a generic repository which handles the saving.
Update looks like this:

public virtual void Update(T entity)
{
    //this.context.Entry(entity).State = EntityState.Modified;

    this.context.SaveChanges();
}

If I uncomment the first line

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

exception is thrown. When commented nothing is saved.

Any help is appreciated.

UPDATE

Ok the problem seems to be that the updated article is not “part” of the context so when I pass it to the update nothing happens. If I get the entity from the repository itself and pass the new values and after that pass this entity everything works as expected. This piece of code actually updates the date in the repository:

var art = this.articleRepository.GetSingle(article.ID);
art.Text = article.Text;
this.articleRepository.Update(art);

What I don’t get is that this works too:

var art = this.articleRepository.GetSingle(article.ID);
art.Text = article.Text;
this.articleRepository.Update(article);

UPDATE 2

Thanks to Vitaliy I now know that attaching is the key, but when I try to attach the new entity I get the same ugly exception

An object with the same key already…

UPDATE 3

As I am not allowed to answer my own question in less than 8h I suppose I have to make another update.

Ok, so this is what I did in order to successfully detach the old and attach the new entity:

public virtual void Update(T entity, object id)
{
    this.context.Entry(this.GetSingle(id)).State = EntityState.Detached;
    this.context.Entry(entity).State = EntityState.Added;
    this.context.Entry(entity).State = EntityState.Modified;

    this.context.SaveChanges();
}

I will think of a better way to pass the ID, as it is already part of the entity, perhaps with and interface “myInterface” that has the ID property in it and T will be of type “myInterface”.

Thanks a lot to Vitaliy.

  • 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-31T15:11:49+00:00Added an answer on May 31, 2026 at 3:11 pm

    You are updating article, which is not attached to Context, thus nothing will be saved.
    Probably you intention was to change DateModified then you should do it like this:

    public ActionResult UpdateArticle(Guid articleID) 
       { 
           if (ModelState.IsValid) 
           { 
               var article =  
                   this.articleRepository.GetSingle(articleID); 
               article.DateModified = DateTime.Now; 
    
               this.articleRepository.Update(article); 
                   return this.Redirect("~/Admin"); 
           } 
           else 
           { 
               return this.Redirect("~/Admin/UnsuccessfulOperation"); 
           } 
       } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this controller in Code Igniter that begins with class MyController extends CI_Controller
Suppose you have the following controller action [HttpPost] public ActionResult Save( CustomerModel model )
So I have this code that takes care of command acknowledgment from remote computers,
Challenge: I have this code that fails to compile. Can you figure out what's
So I have this code that checks 4 parameters (author, title, keyword and subject)
hello I have this code that use to sort a datatable. Dim sortingIndex As
I have this code that performs an ajax call and loads the results into
I have this code that works in a unit test but doesn't work when
I have this code that I want to make point-free; (\k t -> chr
I have this code that fetches some text from a page using BeautifulSoup soup=

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.