2

I am unable to update the entity. Below is the code I used to update the entity. It works the first time its updated. But it fails the next time.

Generic method

 public TEntity Update(TEntity entity)
            {
                _context.Entry(entity).State = EntityState.Modified;
                _context.SaveChanges();
                return entity;
            }

Controller

 public IHttpActionResult Put(Invoice invoice)
        {
            return Ok(invoiceService.UpdateInvoice(invoice));
        }

Service

  public Invoice UpdateInvoice(Invoice invoice)
        {
            return _repo.Update(invoice);
        }

The error that is coming.

System.InvalidOperationException: 'Attaching an entity of type 'Models.Invoice' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.'

Update: See below for the answer.

yala_cat
  • 309
  • 4
  • 21
  • Possible duplicate of [ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value](https://stackoverflow.com/questions/23201907/asp-net-mvc-attaching-an-entity-of-type-modelname-failed-because-another-ent) – Ricardo Pontual Feb 26 '18 at 11:30
  • Need to check your complete code. – Rakesh Burbure Feb 26 '18 at 11:30
  • @RicardoPontual - I will try it out. – yala_cat Feb 26 '18 at 11:48
  • @RicardoPontual - It is not a duplicate question according to the answer given on that it has happened due to some other issue in the code. I have updated my question. Appreciate any feedback given. – yala_cat Feb 26 '18 at 11:59

2 Answers2

0

It looks you haven shared all the necessary code. But the solution is as as below. Your method is taking some action and it is expecting to return some value which is causing the issue.

Here to solve this issue you need to include the code inside Try catch block and handle this exception.

try{
//your code
}
catch(InvalidOperationException){
//you may keep this section blank.
}
Rakesh Burbure
  • 1,045
  • 12
  • 27
  • Return is not causing the issue. The line that's causing the issue is " _context.Entry(entity).State = EntityState.Modified;" – yala_cat Feb 26 '18 at 11:55
  • Is it necessary to return the same object which you got? can you please try removing the return statement and the return type from the method? – Rakesh Burbure Feb 26 '18 at 12:06
  • I jusct checked the detailed error. I had faced this issue earlier. At that time I was trying to access the same record from the two different object. May be you can see your code to check if this is happening somewhere in your code? – Rakesh Burbure Feb 26 '18 at 12:12
  • I checked couldn't find any issue with accessing the object. – yala_cat Feb 26 '18 at 12:15
0

It may be not the best solution available. But for anyone who comes across this issue, I resolved it by re-initializing the context as it was not recognizing the the entity as already available. (By the id)

        public TEntity Update(TEntity entity)
                    {
                        _context = new DbContex();
                        _context.Entry(entity).State = EntityState.Modified;
                        _context.SaveChanges();
                        return entity;
                    }
yala_cat
  • 309
  • 4
  • 21
  • This is a very poor solution. It has a huge side effect, i.e. it overwrites `_context`, which at that point may have changes that will all be lost. The main problem is that `_context`'s life span is too long. It looks like it's a singleton, while its life cycle should be "per request". – Gert Arnold Mar 02 '18 at 09:26
  • @GertArnold - Please suggests the solution you have in mind. Thanks in advance. – yala_cat Mar 02 '18 at 10:56