简体   繁体   中英

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; when adding new object object 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. It should be the error.

You may pass the data for foreign key property or you may change HasRequired() to HasOptional() as of following.

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.

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

Try this way ---

As I assume Product and ProductEligiblity in a one to many relationship.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM