【问题标题】:Pomelo Entity Framework Core Scaffolding fails for MySQL DB "annotation cannot be added because an annotation with the same name already exists."MySQL DB 的 Pomelo Entity Framework Core Scaffolding 失败“无法添加注释,因为已经存在同名的注释。”
【发布时间】:2021-01-27 01:32:06
【问题描述】:

当我尝试使用以下方法构建现有 MySQL 数据库时:

Scaffold-DbContext "server=server;user=user;password=pw;database=db" "Pomelo.EntityFrameworkCore.MySql" -OutputDir EF -f

我收到以下错误:

System.InvalidOperationException: The annotation 'MySql:IndexPrefixLength' cannot be added because an annotation with the same name already exists**.
   at Microsoft.EntityFrameworkCore.Infrastructure.Annotatable.AddAnnotation(String name, Annotation annotation)
   at Microsoft.EntityFrameworkCore.Infrastructure.Annotatable.AddAnnotation(String name, Object value)
   at Microsoft.EntityFrameworkCore.Infrastructure.Annotatable.Microsoft.EntityFrameworkCore.Metadata.IMutableAnnotatable.AddAnnotation(String name, Object value)
   at Microsoft.EntityFrameworkCore.MutableAnnotatableExtensions.AddAnnotations(IMutableAnnotatable annotatable, IEnumerable`1 annotations)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitIndex(EntityTypeBuilder builder, DatabaseIndex index)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitIndexes(EntityTypeBuilder builder, ICollection`1 indexes)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitTable(ModelBuilder modelBuilder, DatabaseTable table)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitTables(ModelBuilder modelBuilder, ICollection`1 tables)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.VisitDatabaseModel(ModelBuilder modelBuilder, DatabaseModel databaseModel)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.Create(DatabaseModel databaseModel, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineerScaffolder.ScaffoldModel(String connectionString, DatabaseModelFactoryOptions databaseOptions, ModelReverseEngineerOptions modelOptions, ModelCodeGenerationOptions codeOptions)
   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_0.<.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)
The annotation 'MySql:IndexPrefixLength' cannot be added because an annotation with the same name already exists.*

我已经成功地使用 Microsoft 包在同一个项目中为同一个数据库搭建了脚手架,没有任何问题。

【问题讨论】:

  • 哪个提供商版本?
  • &lt;PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.8"&gt; &lt;PrivateAssets&gt;all&lt;/PrivateAssets&gt; &lt;IncludeAssets&gt;runtime; build; native; contentfiles; analyzers; buildtransitive&lt;/IncludeAssets&gt; &lt;/PackageReference&gt; &lt;PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.2.2" /&gt; &lt;PackageReference Include="Pomelo.EntityFrameworkCore.MySql.Design" Version="1.1.2" /&gt;

标签: entity-framework-core scaffolding pomelo-entityframeworkcore-mysql db-first


【解决方案1】:

Pomelo 团队已确认该问题是一个错误。 如果一列上有多个索引并且它们都使用索引前缀,则会发生此异常。很快就会发布修复:

https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/issues/1189

【讨论】:

    猜你喜欢
    • 2014-11-23
    • 1970-01-01
    • 2018-08-02
    • 2013-03-07
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多