Page 1 of 1

Duplicate indices from migration

Posted: Fri 15 Sep 2017 15:42
by Developer
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?

Re: Duplicate indices from migration

Posted: Fri 22 Sep 2017 13:39
by Pinturiccio
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.

Re: Duplicate indices from migration

Posted: Mon 25 Sep 2017 08:55
by Developer
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.

Re: Duplicate indices from migration

Posted: Wed 27 Sep 2017 15:57
by Shalex
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/168 ... 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.