0

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: Attaching an entity of type 'WebLanguageTeacher.Models.MyDatabase.Word' 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.

This is how it looks like:

var getWordfromDB = db.Words.Find(word.ID);
word.NextReview = getWordfromDB.NextReview;
word.LastReviewed = getWordfromDB.LastReviewed;

db.Entry(word).State = EntityState.Modified;

I'm making some changes in "Word" object. For some reasons I have to get data from database and put it again to the same object. Without using Find() method, EntityState.Modified works, but when I placed it there, it throws this error.

How to fix that?

Piotrek
  • 10,919
  • 18
  • 73
  • 136
  • can you post you `Word` class details? – Hamid Pourjam Feb 02 '15 at 08:43
  • I can, but I think it's not the point. This is typical class with some string variables. I think this is more EF problem – Piotrek Feb 02 '15 at 08:46
  • if EF had a problem many people have found it earlier than you. your question details is incomplete. we can not help you without knowing the details. – Hamid Pourjam Feb 02 '15 at 08:47
  • 1
    You use `word`'s ID to get `getWordFromDB` but then you modify `word`. What's the point of `getWordFromDB` then (you say "for some reasons", what reasons)? And how do you get `word` in the first place? – Wiktor Zychla Feb 02 '15 at 08:48
  • @dotctor, WiktorZychla Here is the class and "the problem". There is as issue with DateTime, so while getting it from form I receive null DateTime values. C# gets datetime2 value, ef wants datetime value, so get an error. http://stackoverflow.com/questions/28062192/the-conversion-of-a-datetime2-data-type-to-a-datetime-data-type-resulted-in-an-o – Piotrek Feb 02 '15 at 09:03
  • Find method first checks in datastore if the item is present else it fires query. So i think word was previously fetched and that is available in dataStore so getWordFromDB and word both are pointing to same object. So use db.Words.First(x=>x.Id==word.ID);, It may help you – Ankush Jain Feb 02 '15 at 09:05
  • @AnkushJain, still doesn't work – Piotrek Feb 02 '15 at 09:09

1 Answers1

0

The problem appeared because of my wrong understanding of what exactly db.[class].Find() does.

I thought that it simply copies object to the variable, but it's not true. This works as something like link to one of the records in database. The "Word" was somehow marked as "currently modified" so any other attempts to reach this database record was rejected.

I don't know if what I'm saying is correct, but I realized that while debugging, through "trial and error".

Piotrek
  • 10,919
  • 18
  • 73
  • 136