5

I have a table Products and another table ProductDetails:

  CreateTable(
            "dbo.Products",
            c => new
                {
                    Id = c.Guid(nullable: false),
                    // other fields here
                    // ....
                    //....
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.ProductType", t => t.ProductTypeId, cascadeDelete: false)
            .ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: false)
            .Index(t => t.UserId)
            .Index(t => t.ProductTypeId);

 CreateTable(
            "dbo.ProductDetails",
            c => new
                {
                    Id = c.Guid(nullable: false),
                    // other fields here
                    // ....
                    //....
                })
            .PrimaryKey(t => t.Id)                
            .ForeignKey("dbo.Products", t => t.ProductId, cascadeDelete: false)
            .Index(t => t.ProyectoId)

As you can see, when I created this table I disabled cascadeDelete. However, after some development I realized I need to delete a product. Now, my problem is that I can not just delete a product by doing something like this

 var product = getProductById(Id);
_dbContext.Product.Remove(product);
_dbContext.SaveChanges();

How can I do to cascade delete my product table? can I do something like a query for cascadeDelete?

_dbContext.Product.cascadeDelete...   //this is not real code

If it is not possible, how can I alter both my tables Products and ProductDetails.

Thanks

3
  • 4
    Why not enable cascade delete? Commented Aug 14, 2018 at 16:06
  • Yeah, why did you disable it if you need it? You can manually delete the related ProductDetails before deleting the Product records, but that's exactly what delete cascade option is for. Commented Aug 14, 2018 at 16:10
  • 1
    thanks, how can I enable again cascade delete please? Commented Aug 14, 2018 at 16:23

1 Answer 1

4

Solution-1:

With the existing configuration first you have to delete the ProductDetails of the Product before deleting the Product as follows:

List<ProductDetails> productDetailsList =  _dbContext.ProductDetails.Where(pd => pd.ProductId == Id).ToList();
 _dbContext.ProductDetails.RemoveRange(productDetailsList);  

var product = getProductById(Id);
_dbContext.Product.Remove(product);
_dbContext.SaveChanges();

Solution-2:

In your DbContext class rewrite the entity configuration as follows:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
   base.OnModelCreating(modelBuilder);

   modelBuilder.Entity<ProductDetails>().HasRequired(pd => pd.Product).WithMany(p => p.ProductDetails).HasForeignKey(pd => pd.ProductId).WillCascadeOnDelete(true);
}

Now run a new migration and update the database accordingly.

Hope your problem will be solved!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.