when I run a query in EF, the DbContext in it automatically caches the data that it retrieves from the database using query. In some cases it is helpful but I need to to hit database and get in which data changes outside the context. So I did some researches and I saw I need to refresh my dbcontext. I tried it bu it gives following error. How to fix this error?
The refresh attempt has failed because an unexpected entity was returned by the data source.
using (var ctx = new MyDbContext()){
..
var objContext = ((IObjectContextAdapter)ctx).ObjectContext;
var refreshEntities = ctx.ChangeTracker.Entries().Select(x => x.Entity).ToList();
objContext.Refresh(RefreshMode.StoreWins, refreshEntities);
..
}
Context;
public MyDbContext() : base(OraConnection(), true) { }
public DbSet<Books> Book{ get; set; }
public DbSet<Libraries> Library{ get; set; }
I ran into the same issue today. There was a trailing whitespace character in one of the fields in my dataset. That field also happened to be part of the EF Key. The trailing space was unexpected so I removed it in my dataset and the error went away.
I hope that your issue is just as straightforward.