I'm using Web API 2 and Entity Framework 6 and Identity 2
I have product model which relates to an ApplicationUser
model, where I create Product
, I get an error:
Additional information: An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
My model:
public class Product {
public int id { get; set; }
public string url { get; set; }
public string name {get;set}
public ApplicationUser user { get; set; }
}
My create code:
public IHttpActionResult PostProduct(Product product) {
ApplicationUserManager userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(new ApplicationDbContext()));
product.user = userManager.FindById(User.Identity.GetUserId());
if (!ModelState.IsValid) {
return BadRequest(ModelState);
}
db.Products.Add(product);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = product.id }, product);
}