简体   繁体   English

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

[英]“Cannot insert the value NULL into column” despite value not being NULL

I'm using Entity Framework to populate a an SQL database table. 我正在使用Entity Framework填充SQL数据库表。 My object 'WebcpdActivity' has a property 'OwnerIdDsc' that is an int. 我的对象“ WebcpdActivity”具有一个int属性“ OwnerIdDsc”。 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: 在SaveChanges的位置,我得到一个错误:

Cannot insert the value NULL into column 'OwnerIdDsc', table 'CPDActivityRecord.dbo.CPDActivity'; 无法将值NULL插入表'CPDActivityRecord.dbo.CPDActivity'的列'OwnerIdDsc'中; column does not allow nulls. 列不允许为空。 INSERT fails. INSERT失败。 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'. 当我单步执行SaveChanges处的代码时,WebcpdActivity.OwnerIdDsc属性确实为“ 0”而不是“ NULL”。

在此处输入图片说明

EDIT 1*: (edited again to add correct version) 'CPDActivity' class which 'WebcpdActivity' is an instance of 编辑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;
    }
}

EDIT 2 编辑2

added whole of Using block 添加了整个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();
    }        
}

EDIT 3 编辑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. 我们有一个Microsoft CRM安装,其中包含我们需要复制的表,并且仅更新复制的表并扩展其他表。 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. 因此,基本上数据库A中的表A(sb_cpdactivity)已被复制为数据库B中的表B(CPDActivity),并且我已将表C(CPDActivityExtended)添加到数据库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). 因此,当我们保存或创建新记录时,我们仅将其保存到NEW表(表B,C数据库B)中,因此,有必要时可以在原始记录和重复记录之间进行转换。

Table B and C are standalone tables in their own database. 表B和C是各自数据库中的独立表。 Table C has a foreign key that is the primary key of Table B. The webContext only connects to this one database. 表C具有一个外键,该外键是表B的主键。webContext仅连接到该数据库。

Hope this makes sense. 希望这是有道理的。

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. 好的,这花了一段时间,结果证明此错误是由edmx文件引起的,该文件实际上没有映射到错误中引用的列。 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. 我更新了edmx文件,以便所有列都正确映射,并且现在一切都很好。

I think you can't insert a NULL value because you did not declare your int? OwnerIdDsc 我认为您不能插入NULL值,因为您没有声明int? OwnerIdDsc int? OwnerIdDsc variable as being NULLABLE. int? OwnerIdDsc变量为NULLABLE。 Go back to the class properties and see exactly how you declared it. 返回到类属性,并确切地了解如何声明它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM