繁体   English   中英

EF Core 插入具有现有子项的新实体

[英]EF Core insert new entity with existing children

我不确定如何使用 EF Core 插入具有现有子项的新实体。

我在数据库中有一个Products列表。 我正在尝试创建一个包含现有产品列表的新订单。

这是Product class:

public class Product
{
    public long ID { get; set; }

    public string Name { get; set; }
    public string Description { get; set; }

    [Column(TypeName = "decimal(8, 2)")]
    public decimal Price { get; set; }

    public string Category { get; set; }
}

这是Order class

public class Order
{
    public long ID { get; set; }
    public string Name { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public string Country { get; set; }
    public bool Giftwrap { get; set; }

    public IEnumerable<Product> Products { get; set; }
}

当我尝试插入包含现有产品的新订单时:

[HttpPost]
public IActionResult Post([FromBody]  Order model)
{
    context.Add(model);
    context.SaveChanges();
    return Ok(model);
}

我得到一个错误:

SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法在表“Products”中为标识列插入显式值。

这是DbContext

using Microsoft.EntityFrameworkCore;

namespace angularsportsstorenetcore2.Models
{
    public class StoreDbContext : DbContext
    {
        public StoreDbContext(DbContextOptions<StoreDbContext> options)
            : base(options) { }

        public DbSet<Product> Products { get; set; }
        public DbSet<Order> Orders { get; set; }
    }
}

在此处输入图像描述

谢谢大家听取了@Selvin 的建议并创建了多对多关系。

   public class OrderProductJunction
    {
        public long Id { get; set; }
        public long OrderId { get; set; }
        public Order Order { get; set; }
        public long ProductId { get; set; }
        public Product Product { get; set; }
    } 



 public class Order
    {
        public long ID { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }
        public string Country { get; set; }
        public bool Giftwrap { get; set; }
        [NotMapped]
        public IEnumerable<Product> Products { get; set; }
        public IEnumerable<OrderProductJunction> OrderProductJunctions { get; set; }

    }

public class Product { public long ID { get; 放; }

    public string Name { get; set; }

    public string Description { get; set; }

    [Column(TypeName = "decimal(8, 2)")]
    public decimal Price { get; set; }

    public string Category { get; set; }

    public IEnumerable<OrderProductJunction> OrderProductJunctions { get; set; }
}

我将 controller 更改为。

 public IActionResult Post([FromBody]  Order model)
        {
            context.Add(model);
            context.SaveChanges();
            foreach (var item in model.Products)
            {
                context.Add(new OrderProductJunction { OrderId = model.ID, ProductId = item.ID });
            }
            context.SaveChanges();
            return Ok(model);
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM