Reputation:
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; }
}
}
Upvotes: 0
Views: 228
Reputation: 24549
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
2.Add a new model in the project
3.Add property in the XXXContext.cs file
4.Add SetSqlGenerator("System.Data.SqlClient", new EntityTableSqlGenerator())
in the Configuration.cs file
5.Run enable-migrations -force
, add-migration tomtest-somechange
, update-database
in the Package Manager console.
6 . Check the table is created correctly
Upvotes: 3