EF Core 6 /与自定义连接表的多对多关系/ORA-00904:标识符无效
嘿,我正在尝试创建多对多关系,如下所示:
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
获取定义的键 BookId
和 CategoryId
。
它还生成 BooksId
和 CategoriesId
(带有 S)。 这是自动生成的,来自类 Book
和 Category
中定义的属性。
我想要实现的是在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论