Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error While Enabling Code-First Migrations On Mobile Services Database

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.

like image 253
Wasabi Fan Avatar asked Apr 07 '14 21:04

Wasabi Fan


People also ask

How do I code my first migration to an existing database?

Run the Add-Migration InitialCreate command in Package Manager Console. This creates a migration to create the existing schema. Comment out all code in the Up method of the newly created migration. This will allow us to 'apply' the migration to the local database without trying to recreate all the tables etc.

How do you use code first when an existing database schema?

To use code-first for an existing database, right click on your project in Visual Studio -> Add -> New Item.. Select ADO.NET Entity Data Model in the Add New Item dialog box and specify the model name (this will be a context class name) and click on Add.

How do you do initial migration?

Adding a Migration So, firstly, you need to create a migration. Open the Package Manager Console from the menu Tools -> NuGet Package Manager -> Package Manager Console in Visual Studio and execute the following command to add a migration. If you are using dotnet Command Line Interface, execute the following command.

What is the difference between code first and database first?

In the code first approach, the programmer has to write the classes with the required properties first, while in the database first approach, the programmer has to create first the database using GUI.


1 Answers

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/

like image 177
Ansary Ans21 Avatar answered Sep 23 '22 15:09

Ansary Ans21