Error While Enabling Code-First Migrations On Mobi

2019-03-18 12:17发布

问题:

I have an Azure Mobile Services project (C# backend) that I recently created and attached to an Azure SQL database. I have been trying to enable Code-First Migrations on that backing database, but it throws errors when I try to update the database.

I ran through all of the conventional steps to enable migrations (Enable-Migrations, Add-Migration). But when I try to Update-Database, it returns the following error:

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

Why is this happening? There aren't any tables in my database, and the project is pretty much the default.

回答1:

Several of the answers about deriving from a custom entity class will work, but they are not the ideal solution. As the EF team (and others) have mentioned, you need to simply add this line to your Context Configuration constructor.

 SetSqlGenerator("System.Data.SqlClient", new EntityTableSqlGenerator());

This will remove your errors when creating migrations and allow you to update the database via powershell command.



回答2:

If you are getting this error on update-database after creating the migration class, Then have a look @ your migration class. The migration class will take primary is clustered index. So remove that from the up() method.

.PrimaryKey(t => t.Id, clustered: false) .Index(t => t.CreatedAt, clustered: true);

If you using it on azure mobile service, do not call 'update-database' manually. refer http://azure.microsoft.com/en-in/documentation/articles/mobile-services-dotnet-backend-how-to-use-code-first-migrations/



回答3:

I was fighting with this problem today for a few hours. Until I found this link:

How to make data model changes to a .NET backend mobile service

If you follow the instructions there, it will definitely work. The main thing is, that the migration will take place, when you hit F5 during a local debugging session.



回答4:

I just had the same issue.

It is caused by the definition of EntityData that is our base class:

public class OrgTest : EntityData
{
    public string Prop1 { get; set; }
}

I replaced EntityData with my own implementation "CustomEntity" where I removed the attribute [Index(IsClustered = true)] on the CreatedAt column:

public abstract class CustomEntity : ITableData
{
   // protected CustomEntity();

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [TableColumn(TableColumnType.CreatedAt)]
    public DateTimeOffset? CreatedAt { get; set; }
    [TableColumn(TableColumnType.Deleted)]
    public bool Deleted { get; set; }
    [Key]   
    [TableColumn(TableColumnType.Id)]
    public string Id { get; set; }
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    [TableColumn(TableColumnType.UpdatedAt)]
    public DateTimeOffset? UpdatedAt { get; set; }
    [TableColumn(TableColumnType.Version)]
    [Timestamp]
    public byte[] Version { get; set; }
} 

and now I inherit from this one:

public class OrgTest : CustomEntity   // EntityData
{
    public string Prop1 { get; set; }
}

Probably I will have troubles further on, but for the time being I can create my model! Hope you can also start like this!



回答5:

See this article:

avoid nightmares using ef first migration in azure mobile services

EntityData define a cluster indext to CreateAt and Id is by default a cluster index, this way it provide an error and you should define only one.