Duplicate indices from migration

Duplicate indices from migration

Postby Developer » Fri 15 Sep 2017 15:42

In my current project we use code-first and we can succesfully create a database. Now we try to introduce migrations. Assuming we start with an empty database, when we create an initialCreate migration, we end up with a lot of duplicate index names (e.g. IX_Id and IX_Model_Id) for the different tables. Setting the AddTableNameInDefaultIndexName property to true or false doesn't have any effect.

The configuration file is:

Code: Select all
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;

            SetSqlGenerator(PgSqlConnectionInfo.InvariantName, new PgSqlEntityMigrationSqlGenerator());

            var config = Devart.Data.PostgreSql.Entity.Configuration.PgSqlEntityProviderConfig.Instance;
            config.CodeFirstOptions.AddTableNameInDefaultIndexName = true;
        }


A sample of the Up() method:

Code: Select all
            CreateTable(
                "dbo.Users",
                c => new
                    {
                        id = c.Guid(nullable: false),
                        client_id = c.Guid(),
                        name = c.String(),
                    })
                .PrimaryKey(t => t.id)
                .ForeignKey("dbo.Clients", t => t.client_id)
                .Index(t => t.id, name: "IX_Id")
                .Index(t => t.client_id, name: "IX_Client_Id");

            CreateTable(
                "dbo.Scenes",
                c => new
                    {
                        id = c.Guid(nullable: false),
                        client_id = c.Guid(),
                        name = c.String(maxLength: 450)
                    })
                .PrimaryKey(t => t.id)
                .ForeignKey("dbo.Clients", t => t.client_id)
                .Index(t => t.id, name: "IX_Id")
                .Index(t => t.client_id, name: "IX_Client_Id")


Especially since a database could already be created succesfully before we tried to use migrations, we suspect the problem to be in the PgSqlEntityMigrationSqlGenerator. We updated to version 7.9.980.0 of PostgreSql.Entity EF6, but that didn't solve the problem.

How could we make sure that indices are created unique automatically using the table name?
Developer
 
Posts: 2
Joined: Fri 15 Sep 2017 15:18

Re: Duplicate indices from migration

Postby Pinturiccio » Fri 22 Sep 2017 13:39

Developer wrote:Setting the AddTableNameInDefaultIndexName property to true or false doesn't have any effect.

The AddTableNameInDefaultIndexName property does not affect index generation in the code. It affects the generation of indexes in the database, based on your code. If you run your project, the class name will be used in the database index name. This property is set to true by default, so you don't need to use it.
Pinturiccio
Devart Team
 
Posts: 2020
Joined: Wed 02 Nov 2011 09:44

Re: Duplicate indices from migration

Postby Developer » Mon 25 Sep 2017 08:55

Thanks for your reply, we found indeed that the table name is added to the name in the database when there is no name-field provided in .Index(). Migrations adds this name to the migrations file by default however. So it turns out that I still have to manually rename many indexes created by migrations or remove the name-attributes.

How is this supposed to be done with migrations?


Addition: I found that adding the Index-attribute to the Id properties removes the name-field provided in .Index() in the migrations file.
Developer
 
Posts: 2
Joined: Fri 15 Sep 2017 15:18

Re: Duplicate indices from migration

Postby Shalex » Wed 27 Sep 2017 15:57

Developer wrote:So it turns out that I still have to manually rename many indexes created by migrations or remove the name-attributes.
How is this supposed to be done with migrations?
1. Please refer to https://stackoverflow.com/questions/16862208/how-to-drop-unique-index-with-entity-framework-code-first-migrations.

Developer wrote:Addition: I found that adding the Index-attribute to the Id properties removes the name-field provided in .Index() in the migrations file.
2. Try using the IndexAttribute(String) constructor to specify the name of index.
Shalex
Devart Team
 
Posts: 7774
Joined: Thu 14 Aug 2008 12:44


Return to dotConnect for PostgreSQL