簡體   English   中英

錯誤無法在 C# 的表中插入標識列的顯式值

[英]Error Cannot insert explicit value for identity column in table in C#

我試圖在數據庫中保存多條記錄,數據正確存儲在列表中,但是當我按下按鈕保存到數據庫中時,它不起作用。

列表中只有一條記錄並按下按鈕 2 沒有任何反應,但列表中有超過 2 條記錄並按下按鈕 2 程序崩潰並出現以下錯誤代碼:

Microsoft.EntityFrameworkCore.DbUpdateException: '更新條目時出錯。 有關詳細信息,請參閱內部異常。

SqlException:當 IDENTITY_INSERT 設置為 OFF 時,無法在表“roll”中插入標識列的顯式值

我的錯誤是什么?

變量:

    int p = 0;
    string x = "";
    decimal xx = 0;
    string v = "";
    decimal vv = 0;
    string f = "";
    decimal ff = 0;
    decimal pp = 0;
    List<int> piece = new List<int>();
    List<string> protein = new List<string>();
    List<string> vegetable = new List<string>();
    List<string> wrapped = new List<string>();
    List<decimal> piecePrice = new List<decimal>();
    List<decimal> proteinPrice = new List<decimal>();
    List<decimal> vegetablePrice = new List<decimal>();
    List<decimal> wrappedPrice = new List<decimal>();

按鈕點擊:

private void button1_Click(object sender, EventArgs e)
{
        piece.Add(Convert.ToInt32(comboBox1.SelectedValue.ToString()));
        protein.Add(comboBox2.SelectedValue.ToString());
        vegetable.Add(comboBox3.SelectedValue.ToString());
        wrapped.Add(comboBox4.SelectedValue.ToString());

        for (int i = 0; i < piece.Count(); i++)
        {
            var p = piece[piece.Count - 1];
            var h = protein[protein.Count - 1];
            var v = vegetable[vegetable.Count - 1];
            var f = wrapped[ wrapped.Count - 1];

            using (var context = new AppDbContext())
            {
                 piecePrice.Add(Convert.ToDecimal(context.piece.SingleOrDefault(x => x.quantity == p)?.price));
                proteinPrice.Add(Convert.ToDecimal(context.protein.SingleOrDefault(x => x.name == h)?.price));
                vegetablePrice.Add(Convert.ToDecimal(context.vegetable.SingleOrDefault(x => x.name == h)?.price));
                wrappedPrice.Add(Convert.ToDecimal(context. wrapped.SingleOrDefault(x => x.name == f)?.price));
            }
        }
}

保存按鈕單擊處理程序:

private void button2_Click(object sender, EventArgs e)
{
        List<rolls> rolls = new List<rolls>();

        for (int i = 0; i < piece.Count(); i++)
        {
            p = piece[i];
            pp = piecePrice[i];
            x = protein[i];
            v = vegetable[i];
            f =  wrapped[i];
            xx = proteinPrice[i];
            vv = vegetablePrice[i];
            ff =  wrappedPrice[i];

            using (var context = new AppDbContext())
            {
                rolls.Add(new rolls()
                    {
                        quantity = p,
                        quantity_price = pp,
                        protein = x,
                        protein_price = xx,
                        vegetable = v,
                        vegetable_price = vv,
                        wrapped = f,
                        wrapped_price = ff
                    });

                context.AddRange(rolls);
                context.SaveChanges();
            }
        }
}

勞斯萊斯表:

 public class rolls
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int rollId { get; set; }

        public int quantity { get; set; }
        public decimal quantity_price { get; set; }
        public string protein { get; set; }

        public decimal protein_price { get; set; }
        public string vegetable { get; set; }

        public decimal vegetable_price { get; set; }
        public string wrapped { get; set; }

        public decimal wrapped_price { get; set; }

    }

我的第一印象是你想在你的勞斯萊斯列表中插入相同的值。 從 for 循環中刪除“using(){...}”代碼行。 只需為插入過程准備您的列表。 當 for 循環結束時,您可以使用 AddRange 函數插入列表數據。

private void button2_Click(object sender, EventArgs e)
{
    List<rolls> rolls = new List<rolls>();
    for (int i = 0; i < piece.Count(); i++)
    {
        p = piece[i];
        pp = piecePrice[i];
        x = protein[i];
        v = vegetable[i];
        f =  wrapped[i];
        xx = proteinPrice[i];
        vv = vegetablePrice[i];
        ff =  wrappedPrice[I];

        rolls.Add(new rolls()
                {
                    quantity = p,
                    quantity_price = pp,
                    protein=x,
                    protein_price=xx,
                    vegetable=v,
                    vegetable_price=vv,
                    wrapped=f,
                     wrapped_price = ff

        });
    }

    using (var context = new AppDbContext())
    {
       context.AddRange(rolls);
       context.SaveChanges();
    }
}

另一種選擇是您要插入帶有標識列(ID 或您的 Rolls 表的列之一)的記錄,但該列不能插入。 請檢查一下。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM