无法在MVC ASP.NET列中插入空值

无法在MVC ASP.NET列中插入空值,asp.net,asp.net-mvc,entity-framework,Asp.net,Asp.net Mvc,Entity Framework,我得到这个错误: 无法在列(教区区域代码)”中插入空值 当我试图删除关系时,为什么会出现错误?我如何修复它?我已经尝试了我知道的一切,但没有成功。我遵循了本教程 代码如下: 教区 公共部分类教区 { [System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)] 公共教区() { this.application=new

我得到这个错误:

无法在列(教区区域代码)”中插入空值

当我试图删除关系时,为什么会出现错误?我如何修复它?我已经尝试了我知道的一切,但没有成功。我遵循了本教程 代码如下:

教区
公共部分类教区
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]
公共教区()
{
this.application=new HashSet();
this.application_history=new HashSet();
this.other_owner=new HashSet();
this.other_owner_history=new HashSet();
}
公共字符串parish_代码{get;set;}
公共字符串parish_desc{get;set;}
公共字符串区域_代码{get;set;}
公共字符串状态_代码{get;set;}
public System.DateTime更新\u dtime{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection应用程序{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection应用程序\u历史记录{get;set;}
公共虚拟区域{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection其他所有者{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection其他所有者\u历史记录{get;set;}
}
area.cs
公共部分类区域
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]
公共区域()
{
this.application=new HashSet();
this.application_history=new HashSet();
this.parish=新HashSet();
}
公共字符串区域_代码{get;set;}
公共字符串区域_desc{get;set;}
公共字符串状态_代码{get;set;}
public System.DateTime更新\u dtime{get;set;}
公共int版本{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection应用程序{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection应用程序\u历史记录{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection教区{get;set;}
}
更新方法
public void UpdateAreaParishes(string[]selected教区,area area ToUpdate)
{
如果(SelectedParishs==null)
{
areaToUpdate.parish=null;
返回;
}
var selectedParishes=新哈希集(selectedParishes);
var areaparishs=新哈希集(areaToUpdate.parish.Select(x=>x.parish_代码));
foreach(可变教区,以分贝教区为单位)
{
if(selectedparishes.Contains(parish.parish_代码))
{
如果(!AreaParishs.Contains(parish.parish_代码))
areaToUpdate.parish.Add(教区);
}
其他的
{
if(区域教区.包含(教区.教区代码))
{
区域更新。教区。移除(教区);
}
}
}
}

你确定你只是在删除记录吗?对我来说,它看起来像是
区域更新.parish.Add(parish)
正在执行,并且该
教区。area_code
null
,因此,当实体框架上下文保存时,您会收到该异常。是的,我正在更新区域记录。在更新该记录时,我可能会添加或删除指向教区记录的链接,在删除教区记录时,它会抛出该链接“parish.area_代码为空“errorRun
从数据库上区域代码为空的教区中选择。它是否返回任何结果?编辑:我假设列实际上是不可为空的,所以这可能是一个愚蠢的想法。更好的问题是:DB列是否不可为空?如果是这样,该信息在您的模型中显示在哪里?不,它不可为null,并且它是parish类中的一个属性。我所有的模型都是使用实体框架生成的,上面的文件就是生成的。
public partial class parish
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public parish()
    {
        this.application = new HashSet<application>();
        this.application_history = new HashSet<application_history>();
        this.other_owner = new HashSet<other_owner>();
        this.other_owner_history = new HashSet<other_owner_history>();
    }

    public string parish_code { get; set; }
    public string parish_desc { get; set; }
    public string area_code { get; set; }
    public string status_code { get; set; }
    public System.DateTime update_dtime { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<application> application { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<application_history> application_history { get; set; }
    public virtual area area { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<other_owner> other_owner { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<other_owner_history> other_owner_history { get; set; }
}
public partial class area
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public area()
    {
        this.application = new HashSet<application>();
        this.application_history = new HashSet<application_history>();
        this.parish = new HashSet<parish>();
    }

    public string area_code { get; set; }
    public string area_desc { get; set; }
    public string status_code { get; set; }
    public System.DateTime update_dtime { get; set; }
    public int version_nbr { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<application> application { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<application_history> application_history { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<parish> parish { get; set; }
}
public void UpdateAreaParishes(string[] selectedParishes, area areaToUpdate)
    {
        if (selectedParishes == null)
        {
            areaToUpdate.parish = null;
            return;
        }
        var selectedParishesHS = new HashSet<string>(selectedParishes);
        var areaParishes = new HashSet<string>(areaToUpdate.parish.Select(x=> x.parish_code));

        foreach (var parish in db.parish)
        {
            if (selectedParishesHS.Contains(parish.parish_code))
            {
                if (!areaParishes.Contains(parish.parish_code))
                    areaToUpdate.parish.Add(parish);
            }
            else
            {
                if (areaParishes.Contains(parish.parish_code))
                {
                    areaToUpdate.parish.Remove(parish);
                }
            }
        }
    }