EF6.0“无法更改关系,因为一个或多个外键属性不可为空” [英] EF6.0 "The relationship could not be changed because one or more of the foreign-key properties is non-nullable"

查看:108
本文介绍了EF6.0“无法更改关系,因为一个或多个外键属性不可为空”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试删除小孩行,我总是得到一个例外。这是一个剪辑:

If I try to delete a "child" row I always get an exception. Here is a snipset:

using (var context = new CompanyContext())
{
    ItemType itemType = context.ItemTypes.FirstOrDefault(i => i.Name == "ServerType");
    ItemTypeItem itemTypeItem = itemType.Items.FirstOrDefault(i => i.Name == "DatabaseServer");
    itemType.Items.Remove(itemTypeItem);
    context.SaveChanges(); <=== exception!
}

以下异常会抛出在 SaveChanges() 方法。

The following exception is thrown on the SaveChanges() method.


无法更改关系,因为一个或多个外键属性为非-nullable当对关系进行更改时,相关的外键属性设置为空值,如果外键不支持空值,则必须定义新关系,外键属性必须为分配另一个非空值,或者不相关对象必须被删除。

"The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted."

实体配置

  public class ItemTypeConfiguration : NamedEntityConfiguration<ItemType>
  {
    public ConfigurationColumn ParentIDColumn;
    public ConfigurationColumn ValidationPatternColumn;
    public ItemTypeConfiguration() : base()
    {
      ParentIDColumn = new ConfigurationColumn() { Name = "ParentID", Ordinal = base.LastOrdinalPosition + 1 };
      ValidationPatternColumn = new ConfigurationColumn() { Name = "ValidationPattern", Length = 1024, Ordinal=base.LastOrdinalPosition + 2};
      this.Property(t => t.ParentID)
        .HasColumnName(ParentIDColumn.Name)
        .HasColumnOrder(ParentIDColumn.Ordinal);
      this.HasOptional(t => t.Parent).WithMany().HasForeignKey(u => u.ParentID).WillCascadeOnDelete(false);
      this.Property(t => t.ValidationPattern)
        .HasColumnName(ValidationPatternColumn.Name)
        .HasColumnOrder(ValidationPatternColumn.Ordinal)
        .HasMaxLength(ValidationPatternColumn.Length);
    }
...


  public class ItemTypeItemConfiguration : NamedEntityConfiguration<ItemTypeItem>
  {
    public ConfigurationColumn ItemTypeIDColumn;
    public ItemTypeItemConfiguration() : base()
    {
      ItemTypeIDColumn = new ConfigurationColumn(){Name="ItemTypeID", IsRequired=true, Ordinal= base.LastOrdinalPosition+1};
      this.Property(t => t.ItemTypeID)
        .HasColumnName(ItemTypeIDColumn.Name)
        .HasColumnOrder(ItemTypeIDColumn.Ordinal);
      this.HasRequired(t => t.ItemType).WithMany(t=>t.Items).HasForeignKey(u => u.ItemTypeID).WillCascadeOnDelete(true);
    }
...

我发现博客,但我没有DeleteObject方法。

I found the blog but I don't have the "DeleteObject" method.

http://blog.clicdata.com/2013/ 7月4日/在操作失败的-的关系,能不能提改变的 - 因为,一个或者更多的最外键 - 属性 - 是 - 非空的/

任何想法?谢谢。

推荐答案

您需要删除ItemTypeItem。
不可能将它从Items列表中删除,因为它不能自己存在,因为它具有引用ItemType(ItemTypeID)的不可空的外键。

You need to delete the ItemTypeItem. It is not possible to just remove it from the Items list as it cannot exist by itself, because it has a non-nullable foreign key referencing ItemType (ItemTypeID).

要删除ItemTypeItem add

To delete the ItemTypeItem add

context.Entry(itemTypeItem).State = EntityState.Deleted;

这篇关于EF6.0“无法更改关系,因为一个或多个外键属性不可为空”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆