当 IDENTITY_INSERT 设置为 OFF 时,EF Core Code First 无法在 table' 中插入标识列的显式值

EF Core Code First Cannot insert explicit value for identity column in table' when IDENTITY_INSERT is set to OFF

我在我的项目中使用代码优先方法。我也在尝试创建动态 Create 动作和动态 Complete(UnitOfWork) 动作。我在函数添加第一个值后收到该错误。 让我向您展示我的功能:

   public void WebFairHallSeatingCreator(int webFairHallID)
        {
            int[] hallSeatingOrderColumnValue = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
            string[] hallSeatingOrderRowValue = { "a", "b", "c", "d", "e", "f", "g" };
            var hallSeatingOrder = new HallSeatingOrder();
            for (int row = 0; row < hallSeatingOrderRowValue.Length; row++)
            {                
                for (int col = 1; col <= hallSeatingOrderColumnValue.Length; col++)
                {
                    if ((hallSeatingOrderRowValue.GetValue(row).ToString() == "c") || (hallSeatingOrderRowValue.GetValue(row).ToString() == "d"))
                    {
                        if ((col == 5) || (col == 6) || (col == 7))
                        {
                            hallSeatingOrder.HallSeatingOrderRowValue = "x";
                            hallSeatingOrder.HallSeatingOrderColumnValue = 15;
                            _unitOfWorkWFH.RepositoryHallSeatingOrder.Create(hallSeatingOrder);
                        }
                        else
                        {
                            hallSeatingOrder.HallSeatingOrderRowValue= hallSeatingOrderRowValue.GetValue(row).ToString();
                            hallSeatingOrder.HallSeatingOrderColumnValue = col;
                            hallSeatingOrder.WebFairHallID = webFairHallID;
                            _unitOfWorkWFH.RepositoryHallSeatingOrder.Create(hallSeatingOrder);
                            _unitOfWorkWFH.Complete();// This is the point

                        }
                    }
                    else
                    {
                        hallSeatingOrder.HallSeatingOrderRowValue = hallSeatingOrderRowValue.GetValue(row).ToString();
                        hallSeatingOrder.HallSeatingOrderColumnValue = col;
                        hallSeatingOrder.WebFairHallID = webFairHallID;
                        _unitOfWorkWFH.RepositoryHallSeatingOrder.Create(hallSeatingOrder);
                        _unitOfWorkWFH.Complete();// This is the point

                    }
                }
            }
        }

这是我的 HallSeatingOrder class :

  public class HallSeatingOrder : Base
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int HallSeatingOrderID { get; set; }
        [Required]
        public int WebFairHallID { get; set; }
        public int? CompanyID { get; set; }
        [Required]
        public int HallSeatingOrderColumnValue { get; set; }
        [Required]
        public string HallSeatingOrderRowValue { get; set; }
        public bool HallSeatingOrderIsSpecial { get; set; }
        public Company Company { get; set; }
        public WebFairHall WebFairHall { get; set; }
    }
    public class HallSeatingOrderConfiguration : IEntityTypeConfiguration<HallSeatingOrder>
    {
        public void Configure(EntityTypeBuilder<HallSeatingOrder> builder)
        {
            builder.HasKey(x => x.HallSeatingOrderID);
            builder.HasOne(x => x.Company)
                .WithMany(x => x.HallSeatingOrders)
                .HasForeignKey(x => x.CompanyID)
                .OnDelete(DeleteBehavior.NoAction);
            builder.HasOne(x => x.WebFairHall)
                .WithMany(x => x.HallSeatingOrders)
                .HasForeignKey(x => x.WebFairHallID)
                .OnDelete(DeleteBehavior.NoAction);
        }
    }

我已经把我的问题都看出来了。 我尝试使用 ValueGeneratedOnAdd()[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)],但其中 none 对我有效。

感谢@IvanStoev,我解决了这个问题。我的模型和配置都没有问题。有一个错误,让我告诉你:

public void WebFairHallSeatingCreator(int webFairHallID)
        {
            int[] hallSeatingOrderColumnValue = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
            string[] hallSeatingOrderRowValue = { "a", "b", "c", "d", "e", "f", "g" };

            for (int row = 0; row < hallSeatingOrderRowValue.Length; row++)
            {                
                for (int col = 1; col <= hallSeatingOrderColumnValue.Length; col++)
                {
                    if ((hallSeatingOrderRowValue.GetValue(row).ToString() == "c") || (hallSeatingOrderRowValue.GetValue(row).ToString() == "d"))
                    {
                        if ((col == 5) || (col == 6) || (col == 7))
                        {
                            var hallSeatingOrder = new HallSeatingOrder();
                            hallSeatingOrder.HallSeatingOrderRowValue = "x";
                            hallSeatingOrder.HallSeatingOrderColumnValue = 15;
                            _unitOfWorkWFH.RepositoryHallSeatingOrder.Create(hallSeatingOrder);
                            _unitOfWorkWFH.Complete();
                        }
                        else
                        {
                            var hallSeatingOrder = new HallSeatingOrder();
                            hallSeatingOrder.HallSeatingOrderRowValue= hallSeatingOrderRowValue.GetValue(row).ToString();
                            hallSeatingOrder.HallSeatingOrderColumnValue = col;
                            hallSeatingOrder.WebFairHallID = webFairHallID;
                            _unitOfWorkWFH.RepositoryHallSeatingOrder.Create(hallSeatingOrder);
                            _unitOfWorkWFH.Complete();

                        }
                    }
                    else
                    {
                        var hallSeatingOrder = new HallSeatingOrder();
                        hallSeatingOrder.HallSeatingOrderRowValue = hallSeatingOrderRowValue.GetValue(row).ToString();
                        hallSeatingOrder.HallSeatingOrderColumnValue = col;
                        hallSeatingOrder.WebFairHallID = webFairHallID;
                        _unitOfWorkWFH.RepositoryHallSeatingOrder.Create(hallSeatingOrder);
                        _unitOfWorkWFH.Complete();

                    }
                }
            }
        }

我需要为每个 for 循环创建 var hallSeatingOrder = new HallSeatingOrder();,仅此而已。 感谢他