2017-05-06 61 views
0

我使用this tutorial以便将xamarin.forms应用程序连接到简单表格。我不能在Azure的数据添加到数据库中,我得到使用Xamarin表格轻松创建表格 - InvalidOperationException

System.InvalidOperationException

错误消息是以下

对项目的插入操作已经在队列。

在以下代码行中发生异常。

await usersTable.InsertAsync(data); 

为了添加一个用户

var user = new User { Username = "username", Password = "password" }; 
bool x = await AddUser(user); 

ADDUSER

public async Task<bool> AddUser(User user) 
     { 
      try 
      { 
       await usersTable.InsertAsync(user); 
       await SyncUsers(); 
       return true; 
      } 
      catch (Exception x) 
      { 
       await new MessageDialog(x.Message.ToString()).ShowAsync(); 
       return false; 
      } 
     } 

SyncUsers()

public async Task SyncUsers() 
     { 
      await usersTable.PullAsync("users", usersTable.CreateQuery()); 
      await client.SyncContext.PushAsync(); 
     } 

其中

IMobileServiceSyncTable<User> usersTable; 
MobileServiceClient client = new MobileServiceClient("url"); 

初始化

var path = Path.Combine(MobileServiceClient.DefaultDatabasePath, "DBNAME.db"); 
var store = new MobileServiceSQLiteStore(path); 
store.DefineTable<User>(); 
await client.SyncContext.InitializeAsync(store, new MobileServiceSyncHandler()); 
usersTable = client.GetSyncTable<User>(); 
+0

你可以分享你的其他代码吗?谢谢!但是,我认为这可能是因为该项目已添加到您的表格中。提醒一下,我没有设置Id的值。 – mindOfAi

回答

1

请检查您的表。您可能已经添加了该项目。另外,我建议你不要为你的实体设置Id属性,因为你可能会插入一个已经存在于你的表中的相同ID。这可能是出现异常的原因。

希望它有帮助!

+0

我停止设置ID,并且得到一个MobileServicePushFailedException。推送操作失败。有关详细信息,请参阅pushResult。我从Azure门户看到我的表,并添加任何项目。 – GeralexGR

+0

对不起,你能添加项目吗?我有点困惑。 – mindOfAi

+0

虽然显示了第一条错误消息对项目的插入操作已经在队列中,但数据库中没有填充任何项目。不,我不能添加任何信息。 – GeralexGR

0

一些调试,你可以这样做:

1)在后端上的诊断日志记录功能和调试后端:https://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/chapter8/developing/#debugging-your-cloud-mobile-backend 2)在MobileServiceClient设置添加一个日志委托处理程序:https://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/chapter3/server/#turning-on-diagnostic-logs

的MobileServicePushFailedException包含一个包含实际错误的内部异常。通常,它是409/412 HTTP错误之一,表示存在冲突。但是,它也可能是404(这意味着客户端请求的内容与Easy Tables中的表名称不匹配)或500(这意味着服务器崩溃,在这种情况下,服务器端诊断日志会指出原因)。

Easy Tables仅仅是一个Node.js服务。

+0

我的InnerException是'{Newtonsoft.Json.JsonReaderException:解析值时遇到意外的字符:C.路径'',第0行,位置0。从应用程序日志错误404.0 - 未找到。您正在查找的资源已被删除,名称已更改或暂时不可用。 – GeralexGR

+0

查看请求的URL。它会像/ tables/users这样的东西 - 如果它是/ tables/users,那么表的名字必须是用户 - 而不是用户。通常,您需要添加或删除s。 –