Cannot insert a null into column MVC ASP.NET

125 views Asked by At

I get this error:

Cannot insert a null into column (parish.area_code)".

Why do I get that error when I am trying to delete the relationship and how can I fix it? I've tried everything I know but no success. I followed this tutorial Updating Related Data with the Entity Framework in an ASP.NET MVC Application (6 of 10) Code Below:

parish.cs

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

area.cs

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

Update Method

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

There are 0 answers