Entity Framework Core 5 和 CosmosDB SQL API - 包含扩展方法不起作用
我已按照以下 EF Core 5 站点中的步骤操作,以便存储和读取 CosmosDB 中的项目。这是网站:https://www.learnentityframeworkcore5.com/database-providers/cosmos
存储按预期工作,但是当运行教程中的命令来读取项目时,我收到以下错误:
System.InvalidOperationException:“不支持包括导航‘Navigation: Author.Books (List) Collection ToDependent Book Inverse: Author’,因为导航未嵌入到同一资源中。”
引发异常的代码行:
var list = context.Authors
.Include(a => a.Books)
.ToList();
EF Core 5 for CosmosDB 似乎不支持 Include 方法。有人对这个问题有一些经验吗?
I have followed the steps from the following EF Core 5 site in order to store and read items from the CosmosDB. Here is the site: https://www.learnentityframeworkcore5.com/database-providers/cosmos
Storing works as expected, but when running the command from the tutorial to read items, I'm getting following error:
System.InvalidOperationException: 'Including navigation 'Navigation: Author.Books (List) Collection ToDependent Book Inverse: Author' is not supported as the navigation is not embedded in same resource.'
The line of code which throws the exception:
var list = context.Authors
.Include(a => a.Books)
.ToList();
It seems that Include method is not supported in EF Core 5 for CosmosDB. Does anyone have some experience with this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对我来说解决这个问题的方法是删除上下文类中单独的
DbSet
,例如DbSet
。通过它自己的DbSet
,我的对象被映射到单独的文档;如果没有它,它们将存储在包含文档的数组中(例如Author
)。我想您想根据除作者之外的其他信息来查询书籍,但就我而言,我认为我能够使用它。
What fixed it for me was removing the separate
DbSet
in the context class, e.g.DbSet<Book>
. With its ownDbSet
, my objects were being mapped to separate documents; without it, they're stored inside an array in the containing document (e.g.Author
).I imagine you want to query books based on other things besides their authors, but in my case I think I'll be able to work with it.
我会看一下 Microsoft.EntityFrameworkCore.Cosmos。
因为它提供了一种将作者映射到书籍的方法
https://learn.microsoft.com/en-us/ef/core/providers/cosmos/planetary-docs-sample
https://learn.microsoft.com/en-us/ef/core/modeling/relationships?tabs= Fluent-api%2C Fluent-api-simple-key%2Csimple-key
I would take a look at the Microsoft.EntityFrameworkCore.Cosmos instead.
As it provides a means to map the Authors to Books
https://learn.microsoft.com/en-us/ef/core/providers/cosmos/planetary-docs-sample
https://learn.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key