简体   繁体   English

实体框架无法更改关系,因为一个或多个外键属性不可为空

[英]Entity Framework The relationship could not be changed because one or more of the foreign-key properties is non-nullable

I have done the following configuration 我做了以下配置

public class ProductEligibiltyConfiguration : EntityTypeConfiguration<ProductEligibilty>
{
    public ProductEligibiltyConfiguration()
    {
        HasKey(t => t.Id).ToTable("ecom_ProductEligibilty");
        HasRequired(a => a.Product)
            .WithMany(t => t.MeetingProductEligibilty)
            .HasForeignKey(k => k.ProductId)
            .WillCascadeOnDelete(false);
        HasRequired(t => t.MemberType).WithMany().HasForeignKey(k => k.MemberTypeId).WillCascadeOnDelete(false);
    }
}

but getting the error while adding, deleting, updating the table. 但在添加,删除,更新表时收到错误。

Using following code to update while I have set all the references and objects for them as well. 使用以下代码进行更新,同时我也为它们设置了所有引用和对象。

foreach (var eligibleProduct in productEligibiltyes)
        {
          ProductEligibiltys.Attach(eligibleProduct);
          Entry(eligibleProduct).State = EntityState.Modified;
          ProductEligibiltys.Add(eligibleProduct);
       }

Getting error : The operation failed: 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. 如果外键不支持空值,则必须定义新关系,必须为外键属性分配另一个非空值,或者必须删除不相关的对象。

I have noticed a bug in your code which might fix the issue. 我注意到您的代码中存在可能解决问题的错误。 Actually you are doing Entry(eligibleProduct).State = EntityState.Modified; 实际上你正在做Entry(eligibleProduct).State = EntityState.Modified; when adding new object object ProductEligibiltys.Add(eligibleProduct); 添加新对象时ProductEligibiltys.Add(eligibleProduct);

You should remove this line. 你应该删除这一行。 Final code will look like this: 最终代码如下所示:

foreach (var eligibleProduct in productEligibiltyes)
   {
      ProductEligibiltys.Attach(eligibleProduct);
      ProductEligibiltys.Add(eligibleProduct);
   }

In your fluent API you have added configuration as HasRequired() for both foreign keys which expecting non null data for foreign key, But according to the error saying in the eligibleProduct instance the foreign key property should not have the data. 在你的流畅API已添加的配置HasRequired()用于其预期外键非空数据,但根据错误说在外国键eligibleProduct实例外键属性不应该有这些数据。 It should be the error. 应该是错误。

You may pass the data for foreign key property or you may change HasRequired() to HasOptional() as of following. 您可以传递外键属性的数据,也可以将HasRequired()更改为HasOptional() ,如下所示。

public class ProductEligibiltyConfiguration : EntityTypeConfiguration<ProductEligibilty>
{
    public ProductEligibiltyConfiguration()
    {
        HasKey(t => t.Id).ToTable("ecom_ProductEligibilty");
        HasOptional(a => a.Product)
            .WithMany(t => t.MeetingProductEligibilty)
            .HasForeignKey(k => k.ProductId)
            .WillCascadeOnDelete(false);
        HasOptional(t => t.MemberType).WithMany().HasForeignKey(k => k.MemberTypeId).WillCascadeOnDelete(false);
    }
}

Still I don't have much idea about your entities. 我对你的实体还不太了解。 Let me assume as follows 我假设如下

Product and ProductEligiblity in a one to many relationship.For your eligibleProduct instance you will have one property call ProductId and another property call Product(which is an instance of Product entity).for that product instace there will be another property call productID.I am asking you to pass data to that as well.This should work if your entities are correct. 产品和ProductEligibity在一对多关系中。对于您的qualifiedProduct实例,您将有一个属性调用ProductId,另一个属性调用Product(这是Product实体的实例)。对于该产品实例,将有另一个属性调用productID。我是要求你将数据传递给它。如果你的实体是正确的,这应该有用。

NB:If you still getting the error something wrong in your entities or fluent api somewhere and you can diagram the database and check are you getting relationships as per your requirement 注意:如果你的实体或流畅的api中的错误仍然存​​在错误,你可以绘制数据库图表并检查你是否按照你的要求获得了关系

Try this way --- 试试这种方式---

As I assume Product and ProductEligiblity in a one to many relationship. 因为我假设产品和ProductEligiblity在一对多关系中。

foreach (var eligibleProduct in productEligibiltyes)
        { 
          Products products= new Products();
          products.ProductId=eligibleProduct.ProductId;
          products.ProductEligiblity.Add(eligibleProduct);
          Product.Attach(products);
          Entry(products).State = EntityState.Modified;
          Product.Add(products);
       }

暂无
暂无

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

相关问题 实体框架5无法更改该关系,因为一个或多个外键属性不可为空 - Entity Framework 5 The relationship could not be changed because one or more of the foreign-key properties is non-nullable EF添加实体:由于一个或多个外键属性不可为空,因此无法更改关系 - EF adding entity: The relationship could not be changed because one or more of the foreign-key properties is non-nullable 尝试更新实体框架中的记录无法更改关系,因为一个或多个外键属性不可为空 - Trying to update record in entity framework The relationship could not be changed because one or more of the foreign-key properties is non-nullable 由于一个或多个外键属性不可为空,因此无法更改该关系 - The relationship could not be changed because one or more of the foreign-key properties is non-nullable Pocos-无法更改关系,因为一个或多个外键属性不可为空 - Pocos - The relationship could not be changed because one or more of the foreign-key properties is non-nullable 操作失败:由于一个或多个外键属性不可为空,因此无法更改该关系asdf - The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable, asdf EF 6.1.1-无法更改关系,因为一个或多个外键属性不可为空 - EF 6.1.1 - The relationship could not be changed because one or more of the foreign-key properties is non-nullable EF 6:插入多个-无法更改关系,因为一个或多个外键属性不可为空 - EF 6: inserting multiple - The relationship could not be changed because one or more of the foreign-key properties is non-nullable 操作失败:无法更改关系,因为一个或多个外键属性不可为空 - The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable 由于一个或多个外键属性不可为空,因此无法更改该关系 - The relationship could not be changed because one or more of the foreign-key properties is non-nullable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM