I am completely confused about using the primary Entity Frameworks migration in Azure Mobile Services.
I follow the article How to make data model changes to the .NET backend mobile service with additional help from Stack Overflow answers, and also read the First steps of code migration and First code changes in command environments and looked at Migrations - under the hood .
In Visual Studio 2015, I create a new Azure Mobile Service, and then allow the migration of the first code by going to the nuget Package Manager Console (PMC) and running Enable-Migrations . Then I create and run the project. Then, to create the database, I create the initial migration using the PMC Add-Migration Initial command and apply it to the PMC Update-Database -Verbose -TargetMigration Initial .
This fails with error message
Cannot create more than one clustered index in table 'MobileService1.TodoItems. Drop the existing clustered index 'PK_MobileService1.TodoItems' before creating another.
Since I used the Verbose flag, I can see the automatically generated SQL and really connect this to the query and run it against the newly minted database, it gives the same error, because the primary key already provides a clustered index.
CREATE TABLE [MobileService1].[TodoItems] ( [Id] [nvarchar](128) NOT NULL, [Text] [nvarchar](max), [Complete] [bit] NOT NULL, [Version] rowversion NOT NULL, [CreatedAt] [datetimeoffset](7) NOT NULL, [UpdatedAt] [datetimeoffset](7), [Deleted] [bit] NOT NULL, CONSTRAINT [PK_MobileService1.TodoItems] PRIMARY KEY ([Id]) ) CREATE CLUSTERED INDEX [IX_CreatedAt] ON [MobileService1].[TodoItems]([CreatedAt])
However, the article warns me of changes: replace Database.SetInitializer(new MobileServiceInitializer()); on MobileService1.WebApiConfig.Register on
var migrator = new System.Data.Entity.Migrations.DbMigrator(new Migrations.Configuration()); migrator.Update();
But after making this change, I get exactly the same error when I run Update-Database -Verbose -TargetMigration Initial in PMC.
Another suggestion from Dominique Alexandre to comment on his question: Run the Azure Mobile Server project locally instead replace Database.SetInitializer(new MobileServiceInitializer()); on MobileService1.WebApiConfig.Register on
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MobileServiceContext, Migrations.Configuration>());
But again, I get exactly the same error.
What should i use? An easy way to transfer Entity Frameworks code names used in Azure Mobile Services?