问题
I have an Azure Mobile Services project that is based off of the offline to do list example:
http://azure.microsoft.com/en-us/documentation/articles/mobile-services-windows-store-dotnet-get-started-offline-data/
The example application with offline data is working as expected. The problem is when I want to modify the ToDoItem with a new field. If I modify the ToDoItem class with a new field (both the Windows app TodoItem class and the AzureMobileService app TodoItem class) with the following:
[JsonProperty(PropertyName = "NewData")]
public byte[] NewData { get; set; }
When I try to add a new item:
await MobileService.SyncContext.PushAsync();
I get the error:
A first chance exception of type 'Microsoft.WindowsAzure.MobileServices.Sync.MobileServicePushFailedException' occurred in mscorlib.dll
Additional information: Push operation has failed. See the PushResult for details.
The PushResult then has this error:
{Microsoft.WindowsAzure.MobileServices.Sync.MobileServiceTableOperationError}
It is my understanding that the Azure Mobile Service supports Dynamic Schema, so I should not have to manually update the database.
Any idea what I am doing wrong?
I am using a .NET backend.
Update 1/31/2015
If I modify the MobileServiceInitializer from:
public class MobileServiceInitializer : DropCreateDatabaseIfModelChanges<MobileServiceContext>
to:
public class MobileServiceInitializer : ClearDatabaseSchemaIfModelChanges<MobileServiceContext>
It will correctly update the database, but of course clears all of my data in the table. Any idea why DropCreateDatabaseIfModelChanges is throwing the error?
回答1:
This may be because Azure Mobile Services doesn't have permission to Drop your database. See this blog here: http://blogs.msdn.com/b/writingdata_services/archive/2014/03/28/mobile-services-net-backend-initializers-and-model-updates.aspx
The blog lays out two options, if this is your problem:
Drop existing tables - Which has all the problems you're talking about
Enable Code First Migration - which allows you to keep your data. Code First migration is a bit tricky, so be prepared for that, but it is one solution for your problem. The Blog above goes into this a bit but this is a more depth demo of doing a Migration in EF: https://msdn.microsoft.com/en-us/data/JJ591621.aspx
来源:https://stackoverflow.com/questions/28169814/mobileservicetableoperationerror-when-adding-new-field