This is how I encountered this issue. First I need to save my Order
which needs a reference to my ApplicationUser
table:
ApplicationUser user = new ApplicationUser();
user = UserManager.FindById(User.Identity.GetUserId());
Order entOrder = new Order();
entOrder.ApplicationUser = user; //I need this user before saving to my database using EF
The problem is that I am initializing a new ApplicationDbContext to save my new Order
entity:
ApplicationDbContext db = new ApplicationDbContext();
db.Entry(entOrder).State = EntityState.Added;
db.SaveChanges();
So in order to solve the problem, I used the same ApplicationDbContext instead of using the built-in UserManager of ASP.NET MVC.
Instead of this:
user = UserManager.FindById(User.Identity.GetUserId());
I used my existing ApplicationDbContext instance:
//db instance here is the same instance as my db on my code above.
user = db.Users.Find(User.Identity.GetUserId());