FineUI 官方论坛
标题:
调用SaveChanges() 出错
[打印本页]
作者:
wx_B0dP0eol
时间:
2020-12-30 17:13
标题:
调用SaveChanges() 出错
本帖最后由 wx_B0dP0eol 于 2020-12-30 17:15 编辑
向数据表批量插入数据时,在循环到了第二次时SaveChanges出错。
错误信息:IDENTITY_INSERT is set to OFF。
这个问题该怎么解决呢。
数据模型:
public int ID { get; set; }
[Display(Name = "日期")]
public string DATE_TIME { get; set; }
[Display(Name = "生产订单")]
public string DIRECTIONS_NO { get; set; }
[Display(Name = "本体番号")]
public string MAIN_PART_S_NO { get; set; }
[Display(Name = "型名")]
public string BASIC_SPECIFICATION { get; set; }
[Display(Name = "仕样")]
public string SPECIFICATION { get; set; }
[Display(Name = "数量")]
public string QUANTITY { get; set; }
[Display(Name = "客户订单")]
public string PO_NO { get; set; }
[Display(Name = "状态")]
public string STATUS { get; set; }
[Display(Name = "开始时间")]
public string START_TIME { get; set; }
[Display(Name = "结束时间")]
public string END_TIME { get; set; }
[Display(Name = "作业员")]
public string OPERATOR { get; set; }
循环批量插入数据:
while (reader.Read())
{
for (int i = 1; i <= 2; i++)
{
Plans.DATE_TIME = reader.GetString(6).Substring(0, 8);
Plans.DIRECTIONS_NO = reader.GetString(0);
Plans.MAIN_PART_S_NO = bthm;
Plans.BASIC_SPECIFICATION = reader.GetString(2);
Plans.SPECIFICATION = reader.GetString(3);
Plans.QUANTITY = "1";
Plans.PO_NO = reader.GetString(5);
Plans.STATUS = "备料计划";
if (ModelState.IsValid)
{
_context.Plans.Add(Plans);
await _context.SaveChangesAsync();
} }
}
作者:
KK.Leung
时间:
2021-1-15 11:54
在ID上添加特性[Key]试试
欢迎光临 FineUI 官方论坛 (https://fineui.com/bbs/)
Powered by Discuz! X3.4