上下文:Web 应用程序 (asp.net) 使用 FormView 编辑学生数据并将其插入 SQL Server DB。错误:
System.Data.SqlClient.SqlException:无法将值 NULL 插入列 'txtSchoolID'、表 'iSAMS.dbo.TblPupilManagementPupils';列不允许空值。插入失败。
我有一个包含FormView
控件的 aspx 页面,该控件包含(编辑、插入和只读)模板和按钮(用于更新和插入数据)。插入使用 onclick 事件处理程序在TblPupilManagementPupils
表中插入新行。
此表包含(在其他列中)具有以下属性的主键:
身份:真 身份种子:1 身份增量:1 数据类型:int 主键:真 允许空值:false
在代码隐藏文件中,我使用 objectcontext 来注册实体集合并提交新对象。见下文:
protected void btnInsertCandidate_onClick(object sender, EventArgs e)
{
//string SchoolCode = ((Label)selectedCandidateFormView.FindControl("lblCandidateKey")).Text;
string FirstName = ((TextBox)selectedCandidateFormView.FindControl("txbFirstName")).Text;
string LastName = ((TextBox)selectedCandidateFormView.FindControl("txbLastName")).Text;
string PreName = ((TextBox)selectedCandidateFormView.FindControl("txbPreName")).Text;
string Gender = ((TextBox)selectedCandidateFormView.FindControl("txbGender")).Text;
string DOB = ((TextBox)selectedCandidateFormView.FindControl("txbCandidateDOB")).Text;
string YoE = ((TextBox)selectedCandidateFormView.FindControl("txbCandidateYearOfEntry")).Text;
string NCEY = ((DropDownList)selectedCandidateFormView.FindControl("ddlCandidateNCEntryYear")).SelectedValue.ToString();
string BoardingType = ((DropDownList)selectedCandidateFormView.FindControl("ddlCandidateBoardingType")).SelectedValue.ToString();
string Languages = ((TextBox)selectedCandidateFormView.FindControl("txbCandidateLanguage")).Text;
string Nationalities = ((TextBox)selectedCandidateFormView.FindControl("txbCandidateNationality")).Text;
// insert a new candidate into iSAMS (pupil record) via LINQ. Effectively copying a candidate from Admissions into a new pupil row in iSAMS.
using (AdmissionsVerificationApplication.iSAMSEntities iSAMSContext = new AdmissionsVerificationApplication.iSAMSEntities())
{
TblPupilManagementPupil pupil = new TblPupilManagementPupil()
{
txtForename = FirstName,
txtSurname = LastName,
txtPreName = PreName,
txtGender = Gender,
txtDOB = Convert.ToDateTime(DOB),
intEnrolmentSchoolYear = Convert.ToInt32(YoE),
intEnrolmentNCYear = Convert.ToInt32(NCEY),
txtType = BoardingType,
txtLanguage = Languages,
txtNationality = Nationalities
};
iSAMSContext.TblPupilManagementPupils.AddObject(pupil);
iSAMSContext.SaveChanges();
}
}
我还检查了 .edmx 文件以确保 ID 列在 edmx:StorageModels 标记下具有正确的属性:
<EntityType Name="TblPupilManagementPupils">
<Key>
<PropertyRef Name="TblPupilManagementPupilsID" />
</Key>
<Property Name="TblPupilManagementPupilsID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
……
为了完整起见(我认为!),这是生成的 edmx 设计器文件中的相关部分:
public partial class TblPupilManagementPupil : EntityObject
{
#region Factory Method
/// <summary>
/// Create a new TblPupilManagementPupil object.
/// </summary>
/// <param name="tblPupilManagementPupilsID">Initial value of the TblPupilManagementPupilsID property.</param>
/// <param name="txtSchoolID">Initial value of the txtSchoolID property.</param>
public static TblPupilManagementPupil CreateTblPupilManagementPupil(global::System.Int32 tblPupilManagementPupilsID, global::System.String txtSchoolID)
{
TblPupilManagementPupil tblPupilManagementPupil = new TblPupilManagementPupil();
tblPupilManagementPupil.TblPupilManagementPupilsID = tblPupilManagementPupilsID;
tblPupilManagementPupil.txtSchoolID = txtSchoolID;
return tblPupilManagementPupil;
}
#endregion
#region Primitive Properties
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int32 TblPupilManagementPupilsID
{
get
{
return _TblPupilManagementPupilsID;
}
set
{
if (_TblPupilManagementPupilsID != value)
{
OnTblPupilManagementPupilsIDChanging(value);
ReportPropertyChanging("TblPupilManagementPupilsID");
_TblPupilManagementPupilsID = StructuralObject.SetValidValue(value);
ReportPropertyChanged("TblPupilManagementPupilsID");
OnTblPupilManagementPupilsIDChanged();
}
}
}
private global::System.Int32 _TblPupilManagementPupilsID;
partial void OnTblPupilManagementPupilsIDChanging(global::System.Int32 value);
partial void OnTblPupilManagementPupilsIDChanged();
......
我在某处读到可能需要设置属性 IsDBGenerated 以指示映射使用 DB 生成 ID .... 但由于这是一个生成的文件,我不确定如何添加该属性。如果确实是这个问题。
任何帮助都将受到极大的欢迎。
问候
巴里