首页 > 解决方案 > 如何为现有 AzureCosmosDB 生成模型类?

问题描述

我有一个包含所有数据和集合的现有 Azure CosmosDB。现在我正在寻找基于我为 SQLServer 数据库所做的相同过程生成模型类。

https://www.learnentityframeworkcore.com/walkthroughs/existing-database

我使用导航到包含解决方案文件的文件夹的 cmd 实用程序尝试了以下命令:

dotnet ef dbcontext scaffold <Azure CosmosDB ConnectionString> Microsoft.EntityFrameworkCore.Cosmos -o Models

在执行上述命令时,我收到以下错误:

System.InvalidOperationException: Unable to find expected assembly attribute named DesignTimeProviderServicesAttribute in provider assembly Microsoft.EntityFrameworkCore.Cosmos. This attribute is required to identify the class which acts as the design-time service provider factory.
   at Microsoft.EntityFrameworkCore.Design.Internal.DesignTimeServicesBuilder.ConfigureProviderServices(String provider, IServiceCollection services, Boolean throwOnError)
   at Microsoft.EntityFrameworkCore.Design.Internal.DesignTimeServicesBuilder.Build(String provider)
   at Microsoft.EntityFrameworkCore.Design.Internal.DatabaseOperations.ScaffoldContext(String provider, String connectionString, String outputDir, String outputContextDir, String dbContextClassName, IEnumerable`1 schemas, IEnumerable`1 tables, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContextImpl(String provider, String connectionString, String outputDir, String outputDbContextDir, String dbContextClassName, IEnumerable`1 schemaFilters, IEnumerable`1 tableFilters, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContext.<>c__DisplayClass0_1.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Unable to find expected assembly attribute named DesignTimeProviderServicesAttribute in provider assembly Microsoft.EntityFrameworkCore.Cosmos. This attribute is required to identify the class which acts as the design-time service provider factory.

谁能帮我知道如何解决这个问题?

标签: .net-coreazure-cosmosdb

解决方案


为了使其工作,数据库提供程序需要包含有关如何进行脚手架的信息。目前,CosmosDB 不支持此功能,因此您将无法使用 CLI 从现有数据库创建模型。

有一个Github 问题开放以改进错误消息以使其更清晰。如果您愿意,可以在该存储库上打开一个问题,请求为 Cosmos 添加该功能。


推荐阅读