Agamemnon
Agamemnon

Reputation: 607

"Cannot insert the value NULL into column" despite value not being NULL

I'm using Entity Framework to populate a an SQL database table. My object 'WebcpdActivity' has a property 'OwnerIdDsc' that is an int. This gets populated (along with all the other properties) with a value shortly before being saved to the database:

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

At the point of SaveChanges I get an error:

Cannot insert the value NULL into column 'OwnerIdDsc', table 'CPDActivityRecord.dbo.CPDActivity'; column does not allow nulls. INSERT fails. The statement has been terminated.

When I step through the code at the point of SaveChanges the WebcpdActivity.OwnerIdDsc property is indeed '0' and not 'NULL'.

enter image description here

EDIT 1*: (edited again to add correct version) 'CPDActivity' class which 'WebcpdActivity' is an instance of

 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;
    }
}

EDIT 2

added whole of Using block

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();
    }        
}

EDIT 3

I tried to keep the code to the areas I though relevant but it seems this issue may be a little more involved than originally thought. Basically; we have a Microsoft CRM installation that has a table we needed to replicate and only update the replicated table and extend with a further table. This table was replicated without any key constraints but with the data types and data constraints. So basically Table A (sb_cpdactivity) in Database A has been replicated as Table B (CPDActivity) in Database B and I have added Table C (CPDActivityExtended) to Database B.

The idea being we read from the original but NOT save to it. So when we save or create a new record we only do it to the NEW table (Table B,C Database B) Hence there is a way of converting between the original and the duplicate, if needs be).

Table B and C are standalone tables in their own database. Table C has a foreign key that is the primary key of Table B. The webContext only connects to this one database.

Hope this makes sense.

Upvotes: 2

Views: 1193

Answers (2)

Agamemnon
Agamemnon

Reputation: 607

Okay, this took a while and it turns out this error was caused by the edmx file not actually having a mapping to the column that was referenced in the error. So when entity framework passed the object over to the database, that column (and several others) were not included and the database was unable to add a row where a non-nullable column was expecting a value.

I updated the edmx file so that ALL of the columns mapped correctly and everything is now fine.

Upvotes: 1

AlinPaul8806
AlinPaul8806

Reputation: 417

I think you can't insert a NULL value because you did not declare your int? OwnerIdDsc variable as being NULLABLE. Go back to the class properties and see exactly how you declared it.

Upvotes: -4

Related Questions