简体   繁体   中英

MobileServiceInvalidOperationException: Error: Bad request

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

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 JsonProperty for 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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM