Entity Framework Core 5 和 CosmosDB SQL API - 包含扩展方法不起作用

发布于 2025-01-11 12:00:18 字数 632 浏览 0 评论 0原文

我已按照以下 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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌︼了一个春 2025-01-18 12:00:18

对我来说解决这个问题的方法是删除上下文类中单独的DbSet,例如DbSet。通过它自己的 DbSet,我的对象被映射到单独的文档;如果没有它,它们将存储在包含文档的数组中(例如 Author)。

我想您想根据除作者之外的其他信息来查询书籍,但就我而言,我认为我能够使用它。

What fixed it for me was removing the separate DbSet in the context class, e.g. DbSet<Book>. With its own DbSet, 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.

新一帅帅 2025-01-18 12:00:18

我会看一下 Microsoft.EntityFrameworkCore.Cosmos。

因为它提供了一种将作者映射到书籍的方法

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
       modelBuilder.Entity<Authors>()
                .HasMany(p => p.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%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

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
       modelBuilder.Entity<Authors>()
                .HasMany(p => p.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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文