I am doing a project where I am supposed to make an eBook store. This is an entity relationship model using which I generated a DB in SQL Server.
Now, while generating the bill using the following code, I am getting the An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
exception in while calling the SaveChanges() method for my Entity Relationship model, (ebs
)
Here is the code. I am maintaining the cart in the session. Also, the user id is kept in the session too.
List<Title> cartItems = (List<Title>)Session["eStoreCart"];
int userid = Int32.Parse(Session["eStoreUserId"].ToString());
User us = ebs.Users.SingleOrDefault(u => u.UserId == userid);
Bill bl = new Bill();
bl.BillAmount = Decimal.Parse(lblBill.Text);
bl.BillDate = DateTime.Now;
foreach (Title item in cartItems)
{
bl.Titles.Add(item);
}
us.Bills.Add(bl);
ebs.SaveChanges();
Response.Redirect("Orders.aspx");
I am totally new to Entity Framework and LINQ. So any help explaining what is going on, and a workaround will be appreciated.