Ends in:
7 DAYS
11 HRS
39 MIN
38 SEC
Ends in:
7 D
11 H
39 M
38 S
Open Closed

Many-To-Many Issue in Abp suite version 8 #6838


User avatar
0
Vipinthachinari created

We are encountering an issue with a project(Single Layer) created using ABP Suite (8.0.4), specifically with tables that have a many-to-many relationship, such as the Book and Category entities. The Book entity is associated with multiple categories. While we are able to create a book with categories successfully, we are unable to delete it. We are receiving an error message stating, "The property 'Books.BooksId' is part of a key and cannot be modified or marked as modified. To change the principal of an existing entity with an identifying foreign key, first delete the dependent, invoke 'SaveChanges', and then associate the dependent with the new principal." Interestingly, the same code worked in a previous version of the project(Single Layer) using ABP Suite 6.0.2.


5 Answer(s)
  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi, I'll test this and write you back asap.

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    I could not reproduce it:

    Can you please share your *DbContext class content and, share the full error logs?

  • User Avatar
    0
    Vipinthachinari created

    I could not reproduce it:

    Can you please share your *DbContext class content and, share the full error logs?

    This is fine but my Issue is, I can't delete Category from a particular Book. We are waiting for your reply because its very urgent.

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    This is fine but my Issue is, I can't delete Category from a particular Book. We are waiting for your reply because its very urgent.

    Oh okay, sorry for the misunderstanding, I thought the problem was being not able to delete books. I'm able to reproduce the problem you stated now.


    To quickly fix this problem for now, you can change the delete behavior in the dbcontext class. So, please open your DbContext class and make the following changes:

            if (builder.IsHostDatabase())
            {
                builder.Entity<Book>(b =>
                {
                    b.ToTable(DbTablePrefix + "Books", DbSchema);
                    b.ConfigureByConvention();
                    b.Property(x => x.Name).HasColumnName(nameof(Book.Name));
                    b.HasMany(x => x.Categories).WithOne().HasForeignKey(x => x.BookId).IsRequired()
    -                   .OnDelete(DeleteBehavior.NoAction);
    +                   .OnDelete(DeleteBehavior.Cascade);
                });
    
                builder.Entity<BookCategory>(b =>
                {
                    b.ToTable(DbTablePrefix + "BookCategory", DbSchema);
                    b.ConfigureByConvention();
    
                    b.HasKey(
                        x => new { x.BookId, x.CategoryId }
                    );
    
                    b.HasOne<Book>().WithMany(x => x.Categories).HasForeignKey(x => x.BookId).IsRequired()
    -                   .OnDelete(DeleteBehavior.NoAction);
    +                   .OnDelete(DeleteBehavior.Cascade);
    
                    b.HasOne<Category>().WithMany().HasForeignKey(x => x.CategoryId).IsRequired()
    -                   .OnDelete(DeleteBehavior.NoAction);
    +                   .OnDelete(DeleteBehavior.Cascade);
    
                    b.HasIndex(
                        x => new { x.BookId, x.CategoryId }
                    );
                });
            }
    
    

    Just change the .OnDelete(DeleteBehavior.NoAction) to .OnDelete(DeleteBehavior.Cascade), and then create a new migration and apply it to the database. Then, your problem should be fixed.

    Note: We have already made this change in the v8.1.0-rc.1+, so you can also consider updating your application.


    Best regards.

  • User Avatar
    0
    Vipinthachinari created

    This is fine but my Issue is, I can't delete Category from a particular Book. We are waiting for your reply because its very urgent.

    Oh okay, sorry for the misunderstanding, I thought the problem was being not able to delete books. I'm able to reproduce the problem you stated now.


    To quickly fix this problem for now, you can change the delete behavior in the dbcontext class. So, please open your DbContext class and make the following changes:

            if (builder.IsHostDatabase()) 
            { 
                builder.Entity<Book>(b => 
                { 
                    b.ToTable(DbTablePrefix + "Books", DbSchema); 
                    b.ConfigureByConvention(); 
                    b.Property(x => x.Name).HasColumnName(nameof(Book.Name)); 
                    b.HasMany(x => x.Categories).WithOne().HasForeignKey(x => x.BookId).IsRequired() 
    -                   .OnDelete(DeleteBehavior.NoAction); 
    +                   .OnDelete(DeleteBehavior.Cascade); 
                }); 
     
                builder.Entity<BookCategory>(b => 
                { 
                    b.ToTable(DbTablePrefix + "BookCategory", DbSchema); 
                    b.ConfigureByConvention(); 
     
                    b.HasKey( 
                        x => new { x.BookId, x.CategoryId } 
                    ); 
     
                    b.HasOne<Book>().WithMany(x => x.Categories).HasForeignKey(x => x.BookId).IsRequired() 
    -                   .OnDelete(DeleteBehavior.NoAction); 
    +                   .OnDelete(DeleteBehavior.Cascade); 
     
                    b.HasOne<Category>().WithMany().HasForeignKey(x => x.CategoryId).IsRequired() 
    -                   .OnDelete(DeleteBehavior.NoAction); 
    +                   .OnDelete(DeleteBehavior.Cascade); 
     
                    b.HasIndex( 
                        x => new { x.BookId, x.CategoryId } 
                    ); 
                }); 
            } 
     
    

    Just change the .OnDelete(DeleteBehavior.NoAction) to .OnDelete(DeleteBehavior.Cascade), and then create a new migration and apply it to the database. Then, your problem should be fixed.

    Note: We have already made this change in the v8.1.0-rc.1+, so you can also consider updating your application.


    Best regards.

    Yes, its working now Thank you for your Support.

Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06