EF Core 6 /与自定义连接表的多对多关系/ORA-00904:标识符无效

发布于 2025-01-14 04:30:43 字数 2107 浏览 2 评论 0原文

嘿,我正在尝试创建多对多关系,如下所示:

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

C# 类与 Oracle 数据库表完全相同。

我的流畅 Api OnModelCreating 方法:

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

}

我总是收到错误 "ORA-00904: \"BooksId\": invalididentifier" 因为 EF Core 正在尝试执行以下 select 语句:

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

因此,它从 OnModelCreating 获取定义的键 BookIdCategoryId

它还生成 BooksIdCategoriesId (带有 S)。 这是自动生成的,来自类 BookCategory 中定义的属性。

我想要实现的是在 OnModelCreating() 中配置实体,以摆脱两个不必要的 b2c.BooksId、b2c.CategoriesId。 我知道 EF Core 有一个命名约定,但我不想重命名 Book2Category 中的列。

这有可能吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文