MobileServiceInvalidOperationException:错误:错误的请求 [英] MobileServiceInvalidOperationException: Error: Bad request

查看:125
本文介绍了MobileServiceInvalidOperationException:错误:错误的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力尝试将数据上传到Azure数据库(移动服务).我在Azure数据库上的数据模型:

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

我在Xamarin Forms中的模型(该类):

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

将数据写入Azure:

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.

当我查看Azure后端时,我会抱怨ID应该填写:

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)

因此,我然后假定我应该添加一个写入Azure的ID字段:

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

但是当我执行此操作时,我会抱怨您无法指定属性ID的值:

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

我试图将数据库字段设置为int和varchar.我尝试更改应用程序中的模型(Test.cs),以便它也将JsonProperty用作ID字段,我尝试将'ID','Id'和'id'用作两个字段Azure和C#,但没有任何效果.它将抱怨必须填写ID或将其留空.

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.

所以我的问题是将记录写入Azure的确切正确方法是什么?我应该在C#中包含ID字段,还是应该更改JsonProperty,...?

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, ... ?

谢谢, 日元

推荐答案

好的,所以我找到了解决方案.当找到的每个信息都在讨论id的int或字符串字段时,它应该是nvarchar(长度255).默认值应设置为(CONVERT([nvarchar](255),newid(),(0))),这样就不必指定任何ID,它们将由Azure自动生成. 因此,一个示例模型:

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:

然后,您可以创建一个与Azure上的模型同名的类,并添加所有字段:

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

最终将数据发送到Azure:

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

这篇关于MobileServiceInvalidOperationException:错误:错误的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆