EF相关实体,试图插入相关ID

时间:2016-05-31 02:18:04

标签: c# asp.net-mvc entity-framework

我有两个相关的表订单订单商品。我需要EF将Order表中的相关id插入OrderItem。

我已经列出了我的模型Order和Orderitem,我认为这种关系是正确的。

但我不知道如何,我是Entityframework的新手。所以我需要一些帮助:0)

 public ActionResult Accept(Order order)
    {

        order.orderDetails = new List<OrderItem>();
        var cart = (List<Item>)Session["cart"];

        var ord = new Order()
        {

            firstName = order.firstName,
            lastName = order.lastName,
            Email = order.Email,
            phone = order.phone,
            postalCode = order.postalCode,
            city = order.city
        };

        foreach (var item in cart)
        {

            var itemOrder = new OrderItem
            {

                orderName = item.Product.productName,
                salePrice = item.Product.consignment.salePrice,
                quantity = item.Quantity,

            };



            order.orderDetails.Add(itemOrder);

            db.Orders.Add(ord);

            // db.OrderItems.Add(itemOrder);







        }

        db.SaveChanges();

        return View();
    }

模型

顺序

public partial class Order
{
    public int orderId { get; set; }
    public DateTime? orderDate { get; set; }
    [DisplayName("Ordre detaljer")]
    public string orderText { get; set; }
    [DisplayName("Fornavn")]
    public string firstName { get; set; }
    [DisplayName("Efternavn")]
    public string lastName { get; set; }
    [DisplayName("E-mail adresse")]
    public string Email { get; set; }
    [DisplayName("Telefon nr")]
    public string phone { get; set; }
    [DisplayName("Postnr")]
    public string postalCode { get; set; }
    [DisplayName("By")]
    public string city { get; set; }

    public List<OrderItem> orderDetails;

    public Order()
    {
        orderDetails = new List<OrderItem>();
    }

    public virtual ICollection<OrderItem> orderItems { get; set; }

}

OrderItem的

public class OrderItem
{
    public int orderItemId { get; set; }
    public string orderName { get; set; }
    public float salePrice { get; set; }
    public int quantity { get; set; }

    //[ForeignKey("orderId")]
    //public virtual Order Order { get; set; }


}

3 个答案:

答案 0 :(得分:0)

请勿手动将订单商品添加到db.OrderItems。假设您正在使用的订单的“OrderDetails”属性被映射(通过EF)到OrderItem表,将该项添加到orderDetails集合应该足以将具有正确外键的记录插入Order

答案 1 :(得分:0)

你的方法正确如下:

您需要先向items首先添加order

var ord = new Order()
    {
         ...
    };
foreach (var item in cart)
{
    var itemOrder = new OrderItem
    {
         ...
     };
   order.orderDetails.Add(itemOrder);
}
db.Orders.Add(ord);

答案 2 :(得分:0)

我找到了解决问题的方法。无论如何,谢谢你们的帮助:0)

解决方案是添加以下行:

 public ActionResult Accept(Order order)
        {

            order.orderItems = new List<OrderItem>();
            var cart = (List<Item>)Session["cart"];


            foreach (var item in cart)
            {
                    var ord = new Order()
            {

                firstName = order.firstName,
                lastName = order.lastName,
                Email = order.Email,
                phone = order.phone,
                postalCode = order.postalCode,
                city = order.city
            };


                var itemOrder = new OrderItem
                {

                    orderName = item.Product.productName,
                    salePrice = item.Product.consignment.salePrice,
                    quantity = item.Quantity,

                };
       ((List<OrderItem>)order.orderItems).Add(itemOrder);
            }

            db.Orders.Add(order);

            db.SaveChanges();

            return View();
        }