0

My backend is built in .NET, and by including a table in the solution, I got the following error:

Cannot create more than one clustered index on table 'dbo.Favorit'. Drop the existing clustered index 'PK_dbo.Favorit' before creating another.

This code was generated after the Add-Migration CreateFavorit and update-database command:

namespace appService.Migrations
{
    using System;
    using System.Collections.Generic;
    using System.Data.Entity.Infrastructure.Annotations;
    using System.Data.Entity.Migrations;

    public partial class CreateFavorit : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.Favorit",
                c => new
                    {
                        Id = c.String(nullable: false, maxLength: 128,
                            annotations: new Dictionary<string, AnnotationValues>
                            {
                                { 
                                    "ServiceTableColumn",
                                    new AnnotationValues(oldValue: null, newValue: "Id")
                                },
                            }),
                        Nome = c.String(),
                        Lat_dest = c.Double(nullable: false),
                        Lon_dest = c.Double(nullable: false),
                        Id_usuario = c.String(),
                        Endereco = c.String(),
                        MeioTransporte = c.String(),
                        Id_usuario_2 = c.String(),
                        Version = c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion",
                            annotations: new Dictionary<string, AnnotationValues>
                            {
                                { 
                                    "ServiceTableColumn",
                                    new AnnotationValues(oldValue: null, newValue: "Version")
                                },
                            }),
                        CreatedAt = c.DateTimeOffset(nullable: false, precision: 7,
                            annotations: new Dictionary<string, AnnotationValues>
                            {
                                { 
                                    "ServiceTableColumn",
                                    new AnnotationValues(oldValue: null, newValue: "CreatedAt")
                                },
                            }),
                        UpdatedAt = c.DateTimeOffset(precision: 7,
                            annotations: new Dictionary<string, AnnotationValues>
                            {
                                { 
                                    "ServiceTableColumn",
                                    new AnnotationValues(oldValue: null, newValue: "UpdatedAt")
                                },
                            }),
                        Deleted = c.Boolean(nullable: false,
                            annotations: new Dictionary<string, AnnotationValues>
                            {
                                { 
                                    "ServiceTableColumn",
                                    new AnnotationValues(oldValue: null, newValue: "Deleted")
                                },
                            }),
                    })
                .PrimaryKey(t => t.Id)
                .Index(t => t.CreatedAt, clustered: true);

        }

        public override void Down()
        {
            DropIndex("dbo.Favorit", new[] { "CreatedAt" });
            DropTable("dbo.Favorit",
                removedColumnAnnotations: new Dictionary<string, IDictionary<string, object>>
                {
                    {
                        "CreatedAt",
                        new Dictionary<string, object>
                        {
                            { "ServiceTableColumn", "CreatedAt" },
                        }
                    },
                    {
                        "Deleted",
                        new Dictionary<string, object>
                        {
                            { "ServiceTableColumn", "Deleted" },
                        }
                    },
                    {
                        "Id",
                        new Dictionary<string, object>
                        {
                            { "ServiceTableColumn", "Id" },
                        }
                    },
                    {
                        "UpdatedAt",
                        new Dictionary<string, object>
                        {
                            { "ServiceTableColumn", "UpdatedAt" },
                        }
                    },
                    {
                        "Version",
                        new Dictionary<string, object>
                        {
                            { "ServiceTableColumn", "Version" },
                        }
                    },
                });
        }
    }
}

Server microsoft-azure, database SQLServer. How to solve this? Or, what is this error? Thank you.

EDIT:

Model Class:

namespace appService.DataObjects
{
    public class Favorit : EntityData
    {
        public string Nome { get; set; }
        public double Lat_dest { get; set; }
        public double Lon_dest { get; set; }
        public string Id_usuario { get; set; }
        public string Endereco { get; set; }
        public string MeioTransporte { get; set; }
        public string Id_usuario_2 { get; set; }

    }
}
Tom Sun - MSFT
  • 24,161
  • 3
  • 30
  • 47
  • Well, the error says you can have only one clustered index in the table. I think it's creating one for `Id` since it's the primary key, and then another for `CreatedAt` as defined there in the migration. I'm not sure why it wants to create it though. Could you share your model class? – juunas Jan 31 '17 at 19:16
  • @juunas sure, i can share. –  Jan 31 '17 at 22:10
  • @juunas is there –  Jan 31 '17 at 22:12

1 Answers1

3

We can include the following code in the Configuration.cs file to resolve it.

public Configuration()
{
   AutomaticMigrationsEnabled = false;
   SetSqlGenerator("System.Data.SqlClient", new EntityTableSqlGenerator());
}

The error message is caused by Entity framework doesn't have an annotation for creating a clustered index that is not a primary key. The mobile SDK manually creates the right SQL statements to set CreateAt as a non-primary key clustered index. More detail info please refer to another SO thread.

Generally, this error message is caused by not running the Mobile Apps/Mobile Services DB generator. Entity Framework does not have an annotation for creating a clustered index that is not a primary key, so the mobile server SDK manually creates the right SQL statements to set CreatedAt as a non-primary key clustered index.

I did a test for it, and it works correctly.The following is my detail steps:

1.Download the mobile project from azure portal

enter image description here

2.Add a new model in the project

enter image description here

3.Add property in the XXXContext.cs file

enter image description here

4.Add SetSqlGenerator("System.Data.SqlClient", new EntityTableSqlGenerator()) in the Configuration.cs file

enter image description here

5.Run enable-migrations -force, add-migration tomtest-somechange, update-database in the Package Manager console.

enter image description here

6 . Check the table is created correctly

enter image description here

Community
  • 1
  • 1
Tom Sun - MSFT
  • 24,161
  • 3
  • 30
  • 47
  • Sun Do I need to delete this created table? For it appears in migrations –  Feb 01 '17 at 11:39