I am trying to add a Cart record into my class ProjectConext which is of type DbConext. The problem is that when I Add the record and try to save it, I get the error that the CustomerID field cannot be null (which it can't), even though as far as I can see I am inserting a non-null value for it. Here is my code for the Model:
public class ShoppingCart
{
ProjectContext _db = new ProjectContext();
public void AddToCart(Video toAdd, Customer CurUser)
{
var CartItem = new Cart
{
CustomerID = CurUser.CustomerID,
Item = toAdd.Title,
Count = 1,
Price = 1.00
};
_db.Carts.Add(CartItem);
_db.SaveChanges();
}
}
I inserted a breakpoint at the line after the Add() and I can see that CartItem has all fields filled, and _db.Carts is MVCProject.Model.Cart as it should be. If anyone has an idea of why this is not adding the record correctly your help would be greatly appreciated. Thanks in advance.