首页 > 解决方案 > 为什么检查角色需要跨分区查询?

问题描述

在 azure 门户上,我创建了 Cosmos DB(核心 (SQL) API),并在其中添加了一个users容器(分区键id)和roles(分区键name)。安装CodeKoenig.AspNetCore.Identity.DocumentDb包。然后,当我尝试通过角色管理器确定数据库中是否存在角色时,我得到一个DocumentClientException: Cross partition query is required but disabled. Please set x-ms-documentdb-query-enablecrosspartition to true, specify x-ms-documentdb-partitionkey, or revise your query to avoid this exception. 这就是我配置身份服务的方式:

services.AddIdentity<Models.Entities.User, DocumentDbIdentityRole>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddDocumentDbStores(options =>
    {
        options.Database = "forumdb";
        options.UserStoreDocumentCollection = "users";
        options.RoleStoreDocumentCollection = "roles";
    })
    .AddDefaultTokenProviders();

引发异常的代码:

var roleManager = serviceProvider.GetRequiredService<RoleManager<DocumentDbIdentityRole>>();
roleManager.RoleExistsAsync(roleName).Result;

Models.Entities.User继承自AspNetCore.Identity.DocumentDb.DocumentDbIdentityUser。我怀疑应该有一个跨分区请求来检查角色是否存在。我做错了什么或如何x-ms-documentdb-query-enablecrosspartition设置true请求RoleManager

标签: c#asp.net-core.net-coreazure-cosmosdbazure-cosmosdb-sqlapi

解决方案


推荐阅读