【问题标题】:Web application throws 'Cannot INSERT null.... column does not allow NULLSWeb 应用程序抛出 'Cannot INSERT null.... 列不允许 NULLS
【发布时间】:2013-03-04 02:02:39
【问题描述】:

上下文: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....但由于这是一个生成的文件,我不确定如何添加财产。如果确实是这个问题。

我们将不胜感激。

问候

巴里

【问题讨论】:

  • 你不是设置txtSchoolID,我发现这是一个外键?
  • 这个例外很有说服力。 txtSchoolID 为空。我认为您只需阅读异常消息就可以节省时间,而不是写这么长的问题
  • 是的,您的问题不是插入,而是在生成的代码中输入了字符串。你说这是EF4?可以升级到 EF5 吗?那里修复了大量错误。

标签: c# asp.net entity-framework-4 identity-column


【解决方案1】:

原因是iSAMS.dbo.TblPupilManagementPupils 表的txtSchoolID 属性未设置。您需要设置学校 id 或设置列以允许NULL

【讨论】:

  • 非常感谢您的回复....确实 txtSchoolID 尚未设置....我实际上将其与 TblPupilManagementPupilsID 混淆了....当然不需要设置!跨度>
猜你喜欢
  • 1970-01-01
  • 2019-09-22
  • 2011-01-12
  • 1970-01-01
  • 2011-04-13
  • 2020-07-01
  • 1970-01-01
  • 2018-06-25
  • 2017-10-17
相关资源
最近更新 更多