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