【问题标题】:MobileServiceInvalidOperationException: Error: Bad requestMobileServiceInvalidOperationException:错误:错误请求
【发布时间】:2015-10-22 23:30:05
【问题描述】:

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

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

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

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

将数据写入 Azure:

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

但是,当我执行此操作时,我会收到以下错误:

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

当我查看 Azure 后端时,我会收到应该填写 ID 的投诉:

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 字段:

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 指定值的抱怨:

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 或将其留空。

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

谢谢, 延特

【问题讨论】:

    标签: c# azure azure-storage azure-mobile-services xamarin.forms


    【解决方案1】:

    好的,我找到了解决方案。虽然找到的每个信息都是关于 id 的 int 或 string 字段,但它应该是 nvarchar(长度 255)。默认值应设置为(CONVERT([nvarchar](255),newid(),(0))),这样做您不必指定任何 ID,它们将由 Azure 自动生成。 所以一个示例模型:

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

    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:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多