Reputation: 1293
SaveChanges is not being reflected in the database
I have the following code
public async Task<int> Post([FromBody] UserEntry userEntry)
{
UserEntry dbUserEntry;
using (DBContext db = new DBContext())
{
// update
dbUserEntry = this.db.UserEntries
.Where(u => u.UserEntryID == userEntry.UserEntryID)
.Include(u => u.EntryPlayers.Select(y => y.Player))
.FirstOrDefault();
dbUserEntry.TeamName = userEntry.TeamName;
dbUserEntry.EntryPlayers = userEntry.EntryPlayers;
//db.Entry(dbUserEntry).State = EntityState.Modified;
return db.SaveChanges();
}
}
I read somewhere that I need to set the state to modified but if I uncomment the line
//db.Entry(dbUserEntry).State = EntityState.Modified;
I get the error:-
An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
Any ideas on how I can get SaveChanges to work?
Upvotes: 0
Views: 101
Reputation: 81
Use Find() method instead of FirstOrDefault()
dbUserEntry = this.db.UserEntries
.Include(u => u.EntryPlayers.Select(y => y.Player))
.Find(u => u.UserEntryID == userEntry.UserEntryID)
Upvotes: 1