L
L
lucky42021-05-05 19:22:47
Entity Framework
lucky4, 2021-05-05 19:22:47

How to remove a tuple from table M-M?

There is a table M-M (ProductCategory), it has two attributes that act as foreign keys and can be designated as null.
That is, when a tuple with a Product and a Category is deleted from the table, then this record (its index / id) will look like this in the database:
ID_NUMBER = null = null

How can I get rid of this in EF CORE?

In this entity, I only have this:

class ProductCategoryConfiguration : IEntityTypeConfiguration<ProductCategory>
    {
        public void Configure(EntityTypeBuilder<ProductCategory> entity)
        {
            entity
                .HasKey(property => new
                {
                    property.ProductId,
                    property.CategoryId
                });

            entity
                .HasOne(property => property.Product)
                .WithMany(property => property.ProductCategories)
                .HasForeignKey(property => property.ProductId)
                .OnDelete(DeleteBehavior.SetNull);

            entity
                .HasOne(property => property.Category)
                .WithMany(property => property.ProductCategories)
                .HasForeignKey(property => property.CategoryId)
                .OnDelete(DeleteBehavior.SetNull);
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-05-05
@vabka

You can choose cascading deletion instead of SetNull.
Or throw an error and remove the intermediate entry

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question