Repository navigation
Closed
Milestone
Description
After upgrade to 2.0.0, the scaffold-dbcontext stop work throwing this exception:
> System.InvalidOperationException: The annotation 'Npgsql:IndexMethod' 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.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.CreateFromDatabaseModel(DatabaseModel databaseModel, Boolean useDatabaseNames)
> at Microsoft.EntityFrameworkCore.Scaffolding.Internal.RelationalScaffoldingModelFactory.Create(String connectionString, IEnumerable`1 tables, IEnumerable`1 schemas, Boolean useDatabaseNames)
> at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ModelScaffolder.Generate(String connectionString, IEnumerable`1 tables, IEnumerable`1 schemas, String projectPath, String outputPath, String rootNamespace, String contextName, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames)
> at Microsoft.EntityFrameworkCore.Design.Internal.DatabaseOperations.ScaffoldContext(String provider, String connectionString, String outputDir, 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 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)
> The annotation 'Npgsql:IndexMethod' cannot be added because an annotation with the same name already exists.
My command (working with 1.1.1 version):
EntityFrameworkCore\Scaffold-DbContext "Host=1.1.1.1;Database=myDB;Username=postgres;Password=myPWD;" -OutputDir "Models" -Force Npgsql.EntityFrameworkCore.PostgreSQL
Dependencies
Npgsql.EntityFrameworkCore.PostgreSQL 2.0.0
Npgsql.EntityFrameworkCore.PostgreSQL.Design 2.0.0-preview1
Reacted by Simon Birrer
Activity
roji commentedon Aug 19, 2017
Can you please provide a minimal SQL schema dump of your PostgreSQL which reproduces this problem?
roji commentedon Aug 19, 2017
Also, I notice that in your dependencies you have Npgsql.EntityFrameworkCore.PostgreSQL.Design - this package no longer exists and isn't required (this is why there isn't a 2.0.0 RTM version of it). Please remove this dependency, it may be causing the issue.
berets76 commentedon Aug 19, 2017
Dependency removed but the exception is the same
berets76 commentedon Aug 19, 2017
Problem found.
Debugging source code, specially GetIndexes method of NpgsqlDatabaseModelFactory.cs, I found that adding indexes with a long name (in my case for example "apparecchiatureregmanutenzoni2") throw the exception.
I changed the code excluding long name indexes and scaffold-dbcontext succeeded.
After the run I found another problem that I dont understand, but may be caused by excluded indexes:
this table
is generated as
abcod, CHARACTER(2) created as char, instead with version 1.1.1 was correctly a string.
I'm not sure issues are related, because Abilitazioni table has no indexes.
berets76 commentedon Aug 19, 2017
More details: the problem couldn't be the name length but more indexes with long names into the same table.
I changed this row in GetIndexes method
table.Indexes.Add(index);to this one
to limit indexes add.
Then I try filtering only these indexes

with this code
and I got the exception, but if I filter each one individually
all works.
jamiepenney commentedon Aug 21, 2017
I have a minimal repro:
This was my bad, I thought I needed an additional index on email to make lookups easier. I ended up with two indexes, user_email_key and user_email_idx. They were causing the duplicate exception.
berets76 commentedon Aug 21, 2017
@jamiepenney this should not be, your indexes have different names, annotation is by name.
I've a lot of tables with more than one index that works fine, but I'm getting this exception only with this, that after scaffolding contains this 4 indexes
Source table is
p.s: no comment on the benefit of some indexes
berets76 commentedon Aug 21, 2017
# Real problem
The real problem is when a table has 2 different index with the same column (one ASC and one DESC); to reproduce, this table works:
and this throw the exception
berets76 commentedon Aug 21, 2017
I think the problem is in EFCore and not here.
Precisely in EFCore.Design project
Seems to be that EFCore check the existing of an index relying on his own properties, not considering their direction (ASC or DESC).
So, in EFCore.PG you could check "indoption" from "pg_index" table and manage it in any way
var columnOptions = reader.GetValueOrDefault<short[]>("indoption");but in EFCore this information is lost.
In 1.1.1 version, index with identical properties were discarded, and only the last was taken (tested).
berets76 commentedon Aug 22, 2017
I made a quick fix to my cloned code, so I can scaffold my existing database.
I added a method to check if the DatabaseTable already contains an index with same fields, and I log a warning to the PM console.
Check
Call and warning
Another simplest way could be to exclude duplicated indexes directly into SQL query, but I prefer the "warning way" so people can check the usefulness of their indexes.
adopilot commentedon Dec 19, 2017
My apologies for spamming but I also have troubles trying to scrafod db.
I did nu get
And from Package Manager Console I am trying to
Scaffold-DbContext "Port=5432;Server=0.0.0.0;Database=discourse;UserId=user;Password=password;" -OutputDir "Models" -Force Npgsql.EntityFrameworkCore.PostgreSQLEnd than I get exception
browrp commentedon Apr 27, 2018
I started experiencing this issue when trying to re scaffold my DbContext in .Net Core 2.0. This is a portion of the dump that I'm getting:
Does anyone have a fix to this?
roji commentedon Apr 28, 2018
Everyone, sorry for not giving this more attention earlier.
@berets76, thanks for the detailed investigation and repro - I can see the issue happening and I can confirm the issue is on the EF Core side. I've submitted dotnet/efcore#11846 for the EF Core to fix this.
However, there may be other scenarios which also trigger this exception. To anyone getting this exception, please check if you have more than one index on the same column(s) (e.g. ascending/descending). If not, please open a new issue with a schema sample that triggers the exception.
Please keep this issue about @berets76 described problem (two indices on same columns).
[-][2.0.0 NuGet released version] Scaffolding error The annotation 'Npgsql:IndexMethod' cannot be added[/-][+]Cannot scaffold if two indices exist on same columns[/+]adopilot commentedon Apr 28, 2018
If some else struggle to find duplicate indexes as I did, here is explanation how to query db https://dba.stackexchange.com/questions/204706/find-tables-with-multiple-indexes-on-same-column
[-]Cannot scaffold if two indices exist on same columns[/-][+]Cannot scaffold if two indices exist on same column[/+]Don't scaffold default index method annotation
roji commentedon May 26, 2018
Everyone, I've just pushed a commit that works around the EF Core issue (by not emitting the IndexMethod annotation unless a non-default method is actually used in the database). This is a satisfactory workaround which should address the issue for pretty much everyone (unless you really are specifying non-default index methods). The fix will be released in 2.0.3 and in the upcoming 2.1.0.