Asp.net mvc 3 MVC3 db.SaveChanges()插入语句错误

Asp.net mvc 3 MVC3 db.SaveChanges()插入语句错误,asp.net-mvc-3,Asp.net Mvc 3,我也同样关注MVC音乐商店 当我创建orderDetails时,出现了内部异常错误 你能帮我一下这是什么意思吗 INSERT语句与外键约束“FK\U ORDERDDETAILS\U Product”冲突。冲突发生在数据库“rentalDB\”表“dbo.Product\”列“productId”中。\r\n该语句已终止 我需要签入SQL Server吗?我不知道为什么会发生这样的错误 你能给我一些建议吗?我给你一些我的密码 请帮帮我。谢谢 public int CreateOrder(Order

我也同样关注MVC音乐商店

当我创建orderDetails时,出现了内部异常错误

你能帮我一下这是什么意思吗

INSERT语句与外键约束“FK\U ORDERDDETAILS\U Product”冲突。冲突发生在数据库“rentalDB\”表“dbo.Product\”列“productId”中。\r\n该语句已终止

我需要签入SQL Server吗?我不知道为什么会发生这样的错误

你能给我一些建议吗?我给你一些我的密码

请帮帮我。谢谢

public int CreateOrder(Order order)
    {
        decimal orderTotal = 0;

        var cartItems = GetCartItems();

        // Iterate over the items in the cart, adding the order details for each
        foreach (var item in cartItems)
        {
            var orderDetail = new OrderDetails
            {
                productId = item.Product.productId,
                orderId = order.orderId,
                unitPrice = item.priceValue,
                rentalPeriod = item.rentalPeriod,
                startDate = item.dateCreated.AddDays(2),
                endDate = item.dateCreated.AddDays(2 + item.rentalPeriod),
                quantity = item.count
            };

            // Set the order total of the shopping cart
            orderTotal += (item.count * item.priceValue);

            db.OrderDetails.Add(orderDetail);

        }

        // Set the order's total to the orderTotal count
        order.total = orderTotal;

        // Save the order
        db.SaveChanges(); //I have error in here!!!

        // Empty the shopping cart
        EmptyCart();

        // Return the OrderId as the confirmation number
        return order.orderId;
    }
这是viewModel

public class ShoppingCartViewModel
{
    public List<Cart> CartItems { get; set; }
    public decimal CartTotal { get; set; }
}
这是产品

 public class Product
{
    [Key] public int productId { get; set; }

    [Required(ErrorMessage = "Please select category")]
    public int categoryId { get; set; }

    [Required(ErrorMessage = "Please fill in model name")]
    [DisplayName("Model name")]
    public String model { get; set; }

    [DisplayName("Description")]
    public String description { get; set; }

    [DisplayName("Original price")]
    public decimal price { get; set; }

    [Required(ErrorMessage = "Please fill in stock of product")]
    [DisplayName("Stock")]
    public int stock { get; set; }
    public virtual Category Category { get; set; }
}

我没有在OrderDetails中设置FK

这就是我以前犯的错误

当我在OrderDetails和Order之间创建FK时,它将起作用

 public class Product
{
    [Key] public int productId { get; set; }

    [Required(ErrorMessage = "Please select category")]
    public int categoryId { get; set; }

    [Required(ErrorMessage = "Please fill in model name")]
    [DisplayName("Model name")]
    public String model { get; set; }

    [DisplayName("Description")]
    public String description { get; set; }

    [DisplayName("Original price")]
    public decimal price { get; set; }

    [Required(ErrorMessage = "Please fill in stock of product")]
    [DisplayName("Stock")]
    public int stock { get; set; }
    public virtual Category Category { get; set; }
}