我收到此错误:
无法在列中插入null(parish.area_code)“。
为什么在尝试删除关系时出现错误,如何解决?我尝试了所有我知道但没有成功的事情。我按照本教程Updating Related Data with the Entity Framework in an ASP.NET MVC Application (6 of 10) 代码如下:
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);
}
}
}
}