MobileServiceInvalidOperationException: Error: Bad request

耗尽温柔 提交于 2020-01-25 10:58:07

问题


I've been struggling for hours to try and upload data to an Azure database (mobile service). My datamodel on the Azure database:

My model in Xamarin Forms (the class):

public class Test
{
    public string Id { get; set; }

    [JsonProperty("XAxis")]
    public int XAxis { get; set; }
    [JsonProperty("YAxis")]
    public int YAxis { get; set; }
}

Writing the data to Azure:

var test = new Test
{
    XAxis = 100,
    YAxis = 200
};
await client.GetTable<Test>().InsertAsync(test);

However when I execute this I will get the following error:

07-31 11:44:39.285 I/MonoDroid(27512): Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException: Error: Bad request.

When I look to the Azure back-end I will get the complaint that the ID should be filled in:

Error: [Microsoft][SQL Server Native Client 10.0][SQL Server]Cannot insert the value NULL into column 'ID', table 'my_db.database.Test'; column does not allow nulls. INSERT fails. (SqlState: 23000, Code: 515)

So I then assumed that I should add an ID field that writes to Azure:

var test = new Test
{
    Id = Convert.ToString(System.Math.Abs((int)(DateTime.Now.Ticks))),
    XAxis = 100,
    YAxis = 200
};
await client.GetTable<Test>().InsertAsync(test);

But when I run this I will get the complaint that you cannot specify a value for the property id:

07-31 11:56:39.030 I/MonoDroid(28240): Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException: Error: A value cannot be specified for property 'id'
07-31 11:56:39.030 I/MonoDroid(28240): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <IL 0x00011, 0x00078>
07-31 11:56:39.030 I/MonoDroid(28240): at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__0 (object) <IL 0x00006, 0x0006b>
07-31 11:56:39.030 I/MonoDroid(28240): at Android.App.SyncContext/<Post>c__AnonStorey0.<>m__0 () <IL 0x0000c, 0x0005b>
07-31 11:56:39.030 I/MonoDroid(28240): at Java.Lang.Thread/RunnableImplementor.Run () <IL 0x00011, 0x00097>
07-31 11:56:39.030 I/MonoDroid(28240): at Java.Lang.IRunnableInvoker.n_Run (intptr,intptr) <IL 0x0000a, 0x000a3>
07-31 11:56:39.030 I/MonoDroid(28240): at (wrapper dynamic-method) object.b57781da-7718-463b-816c-da6f6ddaedad (intptr,intptr) <IL 0x00011, 0x0003b>
07-31 11:56:39.035 W/art     (28240): JNI RegisterNativeMethods: attempt to register 0 native methods for md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable
07-31 11:56:39.037 D/AndroidRuntime(28240): Shutting down VM

I've tried to make the database field an int and a varchar. I've tried to change the model (Test.cs) in my app so that it also uses JsonPropertyfor the ID field, I've tried to use 'ID', 'Id' and 'id' as fields both on Azure and in C# but nothing will work. It will either complain that the ID has to be filled in or that it should be let empty.

So my question is what is the exact correct way to write a record to Azure? Should I include the ID field in C# or shouldn't I, should I change the JsonProperty, ... ?

Thanks, Yenthe


回答1:


Alright so I found the solution. While every information found is talking about either int or string fields for the id it should be a nvarchar (length 255). The default value should be set to (CONVERT([nvarchar](255),newid(),(0))), by doing so you don't have to specify any ID and they will be automatically generated by Azure. So an example model:

You can then create a class, which has the same name as the model on Azure, and add all the fields:

public class Measure
{
    public string Id { get; set; }
    public int XAxis { get; set; }
    public int YAxis { get; set; }
    public int ZAxis { get; set; }
    public int LongSensor { get; set; }
}

And eventually send the data to Azure:

Measure measure = new Measure { XAxis = 100, YAxis = 200, ZAxis = 300, LongSensor = 400 };
await client.GetTable<Measure>().InsertAsync(measure);


来源:https://stackoverflow.com/questions/31743568/mobileserviceinvalidoperationexception-error-bad-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!