尽管值不为NULL,但“无法将值NULL插入列”

时间:2018-10-17 10:52:42

标签: c# sql entity-framework

我正在使用Entity Framework填充SQL数据库表。我的对象“ WebcpdActivity”具有一个int属性“ OwnerIdDsc”。在保存到数据库之前不久,将使用一个值(以及所有其他属性)填充该值:

WebcpdActivity.OwnerIdDsc = 0;
webContext.CPDActivities.Add(WebcpdActivity);
webContext.SaveChanges();

在SaveChanges出现错误:

  

无法将值NULL插入表“ CPDActivityRecord.dbo.CPDActivity”的“ OwnerIdDsc”列中;列不允许为空。 INSERT失败。该声明已终止。

当我单步执行SaveChanges处的代码时,WebcpdActivity.OwnerIdDsc属性确实为“ 0”而不是“ NULL”。

enter image description here

编辑1 * :(再次编辑以添加正确的版本) “ CPDActivity”类,其中“ WebcpdActivity”是

的实例
 public partial class CPDActivity
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public CPDActivity()
    {
        this.CPDActivityExtendeds = new HashSet<CPDActivityExtended>();
    }

    public string ModifiedByName { get; set; }
    public string ModifiedByYomiName { get; set; }
    public string sb_cqmoduleidName { get; set; }
    public string sb_eventdateidName { get; set; }
    public string sb_MemberContactIdYomiName { get; set; }
    public string sb_MemberContactIdName { get; set; }
    public string CreatedOnBehalfByName { get; set; }
    public string CreatedOnBehalfByYomiName { get; set; }
    public string ModifiedOnBehalfByName { get; set; }
    public string ModifiedOnBehalfByYomiName { get; set; }
    public string CreatedByName { get; set; }
    public string CreatedByYomiName { get; set; }
    public System.Guid OwnerId { get; set; }
    public string OwnerIdName { get; set; }
    public string OwnerIdYomiName { get; set; }
    public int OwnerIdDsc { get; set; }
    public Nullable<int> OwnerIdType { get; set; }
    public Nullable<System.Guid> OwningUser { get; set; }
    public Nullable<System.Guid> OwningTeam { get; set; }
    public System.Guid sb_cpdactivityId { get; set; }
    public Nullable<System.DateTime> CreatedOn { get; set; }
    public Nullable<System.Guid> CreatedBy { get; set; }
    public Nullable<System.DateTime> ModifiedOn { get; set; }
    public Nullable<System.Guid> ModifiedBy { get; set; }
    public Nullable<System.Guid> CreatedOnBehalfBy { get; set; }
    public Nullable<System.Guid> ModifiedOnBehalfBy { get; set; }
    public Nullable<System.Guid> OwningBusinessUnit { get; set; }
    public int statecode { get; set; }
    public Nullable<int> statuscode { get; set; }
    public byte[] VersionNumber { get; set; }
    public Nullable<int> ImportSequenceNumber { get; set; }
    public Nullable<System.DateTime> OverriddenCreatedOn { get; set; }
    public Nullable<int> TimeZoneRuleVersionNumber { get; set; }
    public Nullable<int> UTCConversionTimeZoneCode { get; set; }
    public string sb_name { get; set; }
    public Nullable<System.DateTime> sb_ActivityDate { get; set; }
    public Nullable<bool> sb_confirmedbysupervisor { get; set; }
    public Nullable<decimal> sb_CPDHours { get; set; }
    public Nullable<decimal> sb_CPDPoints { get; set; }
    public string sb_FutureDevelopment { get; set; }
    public Nullable<System.DateTime> sb_FutureDevelopmentTargetDate { get; set; }
    public string sb_ReflectedOutcome { get; set; }
    public Nullable<int> sb_StructuredCPD { get; set; }
    public Nullable<int> sb_type { get; set; }
    public Nullable<int> sb_UnstructuredCPD { get; set; }
    public Nullable<System.Guid> sb_MemberContactId { get; set; }
    public Nullable<System.Guid> sb_cqmoduleid { get; set; }
    public Nullable<System.Guid> sb_eventdateid { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<CPDActivityExtended> CPDActivityExtendeds { get; set; }

    public static explicit operator CPDActivity(sb_cpdactivity v)
    {
        CPDActivity sb_cpda = new CPDActivity();
        sb_cpda.ModifiedByName = v.ModifiedByName;
        sb_cpda.ModifiedByYomiName = v.ModifiedByYomiName;
        sb_cpda.sb_cqmoduleidName = v.sb_cqmoduleidName;
        sb_cpda.sb_eventdateidName = v.sb_eventdateidName;
        sb_cpda.sb_MemberContactIdYomiName = v.sb_MemberContactIdYomiName;
        sb_cpda.sb_MemberContactIdName = v.sb_MemberContactIdName;
        sb_cpda.CreatedOnBehalfByName = v.CreatedOnBehalfByName;
        sb_cpda.CreatedOnBehalfByYomiName = v.CreatedOnBehalfByYomiName;
        sb_cpda.ModifiedOnBehalfByName = v.ModifiedOnBehalfByName;
        sb_cpda.ModifiedOnBehalfByYomiName = v.ModifiedOnBehalfByYomiName;
        sb_cpda.CreatedByName = v.CreatedByName;
        sb_cpda.CreatedByYomiName = v.CreatedByYomiName;
        sb_cpda.OwnerId = v.OwnerId;
        sb_cpda.OwnerIdName = v.OwnerIdName;
        sb_cpda.OwnerIdYomiName = v.OwnerIdYomiName;
        sb_cpda.OwnerIdDsc = v.OwnerIdDsc;
        sb_cpda.OwnerIdType = v.OwnerIdType;
        sb_cpda.OwningUser = v.OwningUser;
        sb_cpda.OwningTeam = v.OwningTeam;
        sb_cpda.sb_cpdactivityId = v.sb_cpdactivityId;
        sb_cpda.CreatedOn = v.CreatedOn;
        sb_cpda.CreatedBy = v.CreatedBy;
        sb_cpda.ModifiedOn = v.ModifiedOn;
        sb_cpda.ModifiedBy = v.ModifiedBy;
        sb_cpda.CreatedOnBehalfBy = v.CreatedOnBehalfBy;
        sb_cpda.ModifiedOnBehalfBy = v.ModifiedOnBehalfBy;
        sb_cpda.OwningBusinessUnit = v.OwningBusinessUnit;
        sb_cpda.statecode = v.statecode;
        sb_cpda.statuscode = v.statuscode;
        sb_cpda.VersionNumber = v.VersionNumber;
        sb_cpda.ImportSequenceNumber = v.ImportSequenceNumber;
        sb_cpda.CreatedOn = v.CreatedOn;
        sb_cpda.TimeZoneRuleVersionNumber = v.TimeZoneRuleVersionNumber;
        sb_cpda.UTCConversionTimeZoneCode = v.UTCConversionTimeZoneCode;
        sb_cpda.sb_name = v.sb_name;
        sb_cpda.sb_ActivityDate = v.sb_ActivityDate;
        sb_cpda.sb_confirmedbysupervisor = v.sb_confirmedbysupervisor;
        sb_cpda.sb_CPDHours = v.sb_CPDHours;
        sb_cpda.sb_CPDPoints = v.sb_CPDPoints;
        sb_cpda.sb_FutureDevelopment = v.sb_FutureDevelopment;
        sb_cpda.sb_FutureDevelopmentTargetDate = v.sb_FutureDevelopmentTargetDate;
        sb_cpda.sb_ReflectedOutcome = v.sb_ReflectedOutcome;
        sb_cpda.sb_StructuredCPD = v.sb_StructuredCPD;
        sb_cpda.sb_type = v.sb_type;
        sb_cpda.sb_UnstructuredCPD = v.sb_UnstructuredCPD;
        sb_cpda.sb_MemberContactId = v.sb_MemberContactId;
        sb_cpda.sb_cqmoduleid = v.sb_cqmoduleid;
        sb_cpda.sb_eventdateid = v.sb_eventdateid;

        return sb_cpda;
    }
}

编辑2

添加了using块的全部

using (CPDWebContext webContext = new CPDWebContext())
{
    Guid activityId = Guid.NewGuid();
    sb_cpdactivity blankSb_cpdactivity = new sb_cpdactivity();
    blankSb_cpdactivity = getCPDData.populateActivtyMetaData(blankSb_cpdactivity);

    CPDActivity WebcpdActivity = new CPDActivity();
    WebcpdActivity = (CPDActivity)blankSb_cpdactivity;

    WebcpdActivity.sb_cpdactivityId = activityId;
    WebcpdActivity.CreatedOn = DateTime.Now;
    WebcpdActivity.ModifiedOn = DateTime.Now;
    WebcpdActivity.statecode = 0;
    WebcpdActivity.statuscode = 1;
    WebcpdActivity.sb_MemberContactId = bsavaMember.MemberID;

    decimal minutes = decimal.Parse(ddl_minutes.SelectedValue);
    WebcpdActivity.sb_CPDHours = decimal.Parse(tb_hours.Text) + minutes;

    WebcpdActivity.sb_name = tb_title.Text;
    WebcpdActivity.sb_ActivityDate = Convert.ToDateTime(DateTime.ParseExact(tb_date.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture));

    lbl_notification.Text = bsavaMember.UserName + ",\r\n" + bsavaMember.MemberNumber + ",\r\n" + bsavaMember.MemberID + ",\r\n" + WebcpdActivity.sb_ActivityDate.ToString();

    CPDActivityExtended cpdActivityExtended = new CPDActivityExtended();
    cpdActivityExtended.SubjectArea = tb_area.Text;
    cpdActivityExtended.Notes = tb_notes.Text;
    cpdActivityExtended.Location = tb_location.Text;
    cpdActivityExtended.Username = bsavaMember.UserName;
    cpdActivityExtended.MemberNumber = bsavaMember.MemberNumber;
    cpdActivityExtended.ContactId = bsavaMember.MemberID;
    cpdActivityExtended.CPDActivityId = activityId;
    cpdActivityExtended.id = Guid.NewGuid();
    cpdActivityExtended.ActiveRecord = 1;

    // testing
    int test = WebcpdActivity.OwnerIdDsc;
    WebcpdActivity.OwnerIdDsc = 1;
    //
    webContext.CPDActivities.Add(WebcpdActivity); // error triggered here
    webContext.CPDActivityExtendeds.Add(cpdActivityExtended);

    try
    {
        webContext.SaveChanges();
    }
    catch (Exception exc)
    {
        string error = exc.Message.ToString();
    }        
}

编辑3

尽管相关,但我尝试将代码保留在我所关注的领域,但似乎此问题可能比原先想象的要复杂得多。基本上;我们有一个Microsoft CRM安装,其中包含我们需要复制的表,并且仅更新复制的表并扩展其他表。复制该表时没有任何关键约束,但有数据类型和数据约束。因此,基本上数据库A中的表A(sb_cpdactivity)已被复制为数据库B中的表B(CPDActivity),并且我已将表C(CPDActivityExtended)添加到数据库B。

我们从原著中读取了这个主意,但没有保存下来。因此,当我们保存或创建新记录时,我们仅将其保存到NEW表(表B,C数据库B)中,因此,有一种在原始记录和重复记录之间进行转换的方法(如果需要)。

表B和C是它们自己的数据库中的独立表。表C具有一个外键,该外键是表B的主键。webContext仅连接到该数据库。

希望这很有道理。

2 个答案:

答案 0 :(得分:0)

好吧,这花了一段时间,结果证明此错误是由edmx文件引起的,该文件实际上没有映射到错误中引用的列。因此,当实体框架将对象传递给数据库时,该列(和其他几个列)不包括在内,数据库无法在非空列期望值的行中添加行。

我更新了edmx文件,以便所有列均正确映射,并且现在一切正常。

答案 1 :(得分:-4)

我认为您不能插入NULL值,因为您没有将int? OwnerIdDsc变量声明为NULLABLE。 返回类的属性,并确切地了解您如何声明它。