These are my classes
public class Bill
{
[Key]
public int BillID { get; set; }
[Required]
public int BillNumber { get; set; }
[Required]
public DateTime Date { get; set; }
[Required]
public string UserName { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Adress { get; set; }
[Required]
public string PostalCode { get; set; }
[Required]
public string City { get; set; }
[Required]
public string Country { get; set; }
public Bill()
{
Date = new DateTime();
Date = DateTime.Now;
}
public class Cart
{
[Key]
public int CartID { get; set; }
[Required]
public int BillID { get; set; }
[Required]
[ForeignKey("BillID")]
public virtual Bill Bill { get; set; }
public virtual ICollection<CartItems> Products {get; set;}
}
public class CartItems
{
[Key]
public int CartItemID { get; set; }
[Required]
public Product Product { get; set; }
[Required]
public int Qunatity { get; set; }
}
}
I am buidlding an ASP.NET 4 web application. I have a problem when I want to add products to the cart, it throws an error
An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
when I want to add a cart obejct to the Carts in context.
Bill bill = new Bill();
bill.BillNumber = Bill.GetLastBillNumber(context) + 1;
bill.Adress = txtAdress.Text;
bill.City = txtCity.Text;
bill.Country = ddlCountry.SelectedItem.Value.ToString();
bill.Name = txtName.Text;
bill.PostalCode = txtPostalCode.Text;
bill.UserName = lblUserName.Text;
context.Bills.Add(bill);
context.SaveChanges();
Cart cart = new Cart();
cart.Bill = bill;
cart.BillID = bill.BillID;
cart.Products = Application["CartProducts"] as List<CartItems>;
context.Carts.Add(cart);
context.SaveChanges();
I read about the problem, but I cant do the Detach its not working for me;
the DataContext is definied as privet in the class
private DataContext context = new DataContext();
If anyone can help me please.
Thx.
Did you load the Products in Application[CartProducts]
somewhere else, with another context? In that case that's the problem.
A DataContext
is a Unit of Work implementation, which means that all the work you do in a single operation has to be done on the same context.
While it is generally a good idea to cache the products, it won't work. You will have to create a copy of the objects in the list instead.
cart.Products = (Application["CartProducts"] as List<CartItems>).Select(ci =>
CartItem { ProductId = ci.ProductId, Quantity = ci.Quantity }).ToList();
Note that I have replaced Product
with ProductId
to avoid having to step one more level into the object tree. In the database, the reference between the objects will be implemented as a foreignkey and by mapping that to ProductId
you will be able to access it and use it directly. See this blogpost of mine for details about mapping the foreign key.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.