我是Entity Framework的新手,我已经在google搜索了这个问题,但我什么也没找到。我收到了错误
更新条目时发生错误。有关详细信息,请参阅内部异常。
当我检查内部异常时,我看到了这个
无法将值NULL插入列' DOCNUM',table' TCPIDBV2.dbo.tbl_200_Dailyreport&#39 ;;列不允许空值。 INSERT失败。
但是当我检查我传入的记录以保存在数据库中时,该对象中有一个名为' DOCNUM'
的值这是我的代码:
[Table("tbl_200_Dailyreport")]
public class tbl_200_Dailyreport
{
[Key]
[Required]
public long DOCNUM {get; set;}
public DateTime DAILYREPDATE {get; set;}
public string SECTION {get; set;}
public string REMARKS { get; set; }
public int? OPERATIONTIME { get; set; }
public int? PREPARATIONTIME { get; set; }
public int? STANDBYTIME { get; set; }
public int? MATERIALWAITING { get; set; }
public int? ASSISTTIME { get; set; }
public int? MAINTENANCETIME { get; set; }
public int? CLEANINGTIME { get; set; }
public bool? ISOVERTIME { get; set; }
public bool? ISLOCKED { get; set; }
public string SHIFT { get; set; }
public int? PRODTIME { get; set; }
public int? BREAKTIME { get; set; }
public double? TOTALCOLOR { get; set; }
public double? AVERAGECOLOR { get; set; }
}
获取上述值的代码是:
tbl_200_Dailyreport.DOCNUM = long.Parse(txtReferenceNo.Text);
tbl_200_Dailyreport.PRODTIME = Convert.ToInt32(txtProduction.Text);
tbl_200_Dailyreport.OPERATIONTIME = Convert.ToInt32(txtOperation.Text);
tbl_200_Dailyreport.PREPARATIONTIME = Convert.ToInt32(txtPreparation.Text);
tbl_200_Dailyreport.STANDBYTIME = Convert.ToInt32(txtStandby.Text);
tbl_200_Dailyreport.MAINTENANCETIME = Convert.ToInt32(txtMaintenance.Text);
tbl_200_Dailyreport.CLEANINGTIME = Convert.ToInt32(txtCleaning.Text);
tbl_200_Dailyreport.MATERIALWAITING = Convert.ToInt32(txtMaterial.Text);
tbl_200_Dailyreport.ASSISTTIME = Convert.ToInt32(txtAssist.Text);
tbl_200_Dailyreport.BREAKTIME = Convert.ToInt32(txtOthers.Text);
tbl_200_Dailyreport.DAILYREPDATE = dtpDailyReportDate.Value;
tbl_200_Dailyreport.SHIFT = cboShift.SelectedValue.ToString();
tbl_200_Dailyreport.ISOVERTIME = cbxOvertime.Checked;
tbl_200_Dailyreport.ISLOCKED = false;
tbl_200_Dailyreport.REMARKS = txtRemarks.Text;
if (!String.IsNullOrEmpty(cboSecDepartment.Text))
{
tbl_200_Dailyreport.SECTION = cboSecDepartment.SelectedValue.ToString();
}
else
{
var section = "0";
tbl_200_Dailyreport.SECTION = section;
}
我允许所有列都为空,除了名为' DOCNUM'的PK,因此在获取' DOCNUM'的值时,txtreferenceno.Text
具有值等于1。并在DailyReportManager中传递它,所以这里是代码:
public void Save(tbl_200_Dailyreport records)
{
try
{
this.db.tbl_200_Dailyreport.Add(records);
this.db.SaveChanges();
}
catch (Exception ex)
{
throw ex;
}
}
并获取txtreferenceno.Text的值
var max = dailyreportmanager.FindAll().Max(x => (int?)x.DOCNUM) ?? 0;
var addMax = max + 1;
txtReferenceNo.Text = Convert.ToString(addMax);
所以我的数据库设计
请允许我发布截图:
当我查看记录时,' DOCNUM'有一个值等于1,但为什么我仍然得到这个错误?请帮忙。非常感谢。
答案 0 :(得分:1)
要向Entity Framework指明您要手动指定键值,请指定此属性:
[DatabaseGenerated(DatabaseGeneratedOption.None)]
反对DOCNUM
。