【发布时间】:2018-11-07 23:01:47
【问题描述】:
我正在尝试在 ASP.NET Core 网站中使用新的 EntityFrameworkCore.Cosmos Preview 3 包,但在尝试创建数据库时不断收到错误消息。我似乎找不到任何文档来让我了解导致错误的原因。
在我的 Startup.cs 文件的 ConfigureServices 方法中,我有以下代码:
// Configure EntityFramework - CosmosDB
services.AddDbContext<EFContext>(options => options.UseCosmos(
authKeyOrResourceToken: ServiceFactory.GetDbKeySecret(_config, (_hosting.EnvironmentName != "LocalDev")),
serviceEndPoint: _config.GetValue<string>("CosmosDb:Endpoint"),
databaseName: _config.GetValue<string>("CosmosDb:DatabaseID")
));
EFContext efc = services.BuildServiceProvider().GetRequiredService<EFContext>();
efc.Database.EnsureCreatedAsync().Wait();
当它到达最后一行时,我收到以下错误(堆栈跟踪):
at Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal.CosmosClient.CreateDatabaseIfNotExistsOnceAsync(DbContext _, Object __, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal.CosmosDatabaseCreator.EnsureCreatedAsync(CancellationToken cancellationToken)
而使用 Fiddler 的详细响应是:“{”code”:“NotFound”,“message”:“系统中不存在具有指定 id 的实体。\r\nActivityId: 6aed2df0-abe8-4e93-b468 -b3188005fcb5, Microsoft.Azure.Documents.Common/2.1.0.0"}"
我已经检查并仔细检查了我的配置中的所有值是否正确并且与我在 Azure 中的 CosmosDB 帐户相匹配,但我仍然无法找出导致此错误的原因。
有没有人遇到过这个问题并有解决方案? 谢谢
【问题讨论】:
-
您确定在尝试创建数据库时会发生这种情况吗?该错误消息似乎表明具有指定 id 的文档不存在。
-
绝对是在创建数据库期间,或者更准确地说是在检查数据库是否存在期间(显然)。无论数据库是否存在,我都会收到相同的消息(我在没有数据库的情况下尝试过,然后在门户中创建了一个同名的数据库,然后再次尝试得到相同的结果)。目前还没有涉及文档。
标签: asp.net-core entity-framework-core azure-cosmosdb