'EF Core 6 / Many to many relationship with custom connection table / ORA-00904 : invalid identifier

Hey im trying to create a many to many relationship as following:

public class Book
{
    public int BookId { get; set; }
    public string Title { get; set; }
    public Author Author { get; set; }
    public ICollection<Category> Categories { get; set; }
}  
public class Category
{
    public int CategoryId { get; set; }
    public string CategoryName { get; set; }
    public ICollection<Book> Books { get; set; }
}  
public class Book2Category
{
    public int BookId { get; set; }
    public int CategoryId { get; set; }
}

The c# classes are exactly the same like the oracle database tables.

My fluent Api OnModelCreating method:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Book>(entity =>
    {
       entity.HasMany(b => b.Categories)
             .WithMany(c => c.Books)
             .UsingEntity<Book2Category>(d =>
             {
                d.HasKey(k => new { k.BookId, k.CategoryId});
             });
    });

    modelBuilder.Entity<Category>(entity =>
    {
       entity.HasMany(b => b.Books)
             .WithMany(c => c.Categories)
             .UsingEntity<Book2Category>(d =>
             {
                d.HasKey(k => new { k.BookId, k.CategoryId});
             });
    });

}

I always get error "ORA-00904: \"BooksId\": invalid identifier" because EF Core is trying to execute the following select statement:

/*some selects before*/
left join (
    select b2c.BookId, b2c.CategoryId, b2c.BooksId, b2c.CategoriesId
        from Book2Category b2c
    /*some more after but unimportant*/
)

So it takes the defined Keys BookId and CategoryId from OnModelCreating.

But also it genarates BooksId and CategoriesId (with an S). This is autogenerated and comes from the properties defined in classes Book and Category.

What i want to achieve is to configure the entities in OnModelCreating() that i get rid of the two unnecessary b2c.BooksId, b2c.CategoriesId. I know that there is a name convention for EF Core but i dont want to rename the columns in Book2Category.

Is that possible somehow?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source