AzureMobileService:将数据插入到表中会产生exception

我是新手来实施Azure移动服务。 我参考了Azure提供的ToDoItem演示。

以同样的方式,我已经为我自己的应用程序创建类User。 然后我将数据插入到MobileServiceTable中,但它给出了如下错误:

{"message":"The operation failed with the following error: 'A null store-generated value was returned for a non-nullable member 'CreatedAt' of type 'CrazyLabApp.Models.User'.'."} 

我没有创建任何这样的字段,因为它不是在ToDoItem演示中创建的。 我已经看到MobileServiceTable创建了4个默认字段。 createdAt是其中一个领域。

我很想知道我做错了什么。

检查我的以下用户类:

  public class User { @com.google.gson.annotations.SerializedName("id") private String ServiceUserId; @com.google.gson.annotations.SerializedName("email") private String Email; @com.google.gson.annotations.SerializedName("firstname") private String FirstName; @com.google.gson.annotations.SerializedName("lastname") private String LastName; @com.google.gson.annotations.SerializedName("profilepic") private String ProfilePic; @com.google.gson.annotations.SerializedName("introduction") private String Introduction; @com.google.gson.annotations.SerializedName("website") private String Website; @com.google.gson.annotations.SerializedName("title") private String Title; @com.google.gson.annotations.SerializedName("_createdAt") private Date CreatedAt; @com.google.gson.annotations.SerializedName("coverimage") private ArrayList CoverImages; /*public Date getCreatedAt() { return CreatedAt; } public void setCreatedAt(Date createdAt) { CreatedAt = createdAt; }*/ @com.google.gson.annotations.SerializedName("followers") private ArrayList Followers; @com.google.gson.annotations.SerializedName("likes") private ArrayList Likes; @com.google.gson.annotations.SerializedName("collections") private ArrayList Collections; @com.google.gson.annotations.SerializedName("comments") private ArrayList Comments; @com.google.gson.annotations.SerializedName("stories") private ArrayList Stories ; //-------------- Methods public ArrayList getStories() { return Stories; } public void setStories(ArrayList stories) { Stories = stories; } public ArrayList getComments() { return Comments; } public void setComments(ArrayList comments) { Comments = comments; } public ArrayList getCollections() { return Collections; } public void setCollections(ArrayList collections) { Collections = collections; } public ArrayList getLikes() { return Likes; } public void setLikes(ArrayList likes) { Likes = likes; } public ArrayList getFollowers() { return Followers; } public void setFollowers(ArrayList followers) { Followers = followers; } public ArrayList getCoverImages() { return CoverImages; } public void setCoverImages(ArrayList coverImages) { CoverImages = coverImages; } public String getTitle() { return Title; } public void setTitle(String title) { Title = title; } public String getWebsite() { return Website; } public void setWebsite(String website) { Website = website; } public String getIntroduction() { return Introduction; } public void setIntroduction(String introduction) { Introduction = introduction; } public String getLastName() { return LastName; } public void setLastName(String lastName) { LastName = lastName; } public String getProfilePic() { return ProfilePic; } public void setProfilePic(String profilePic) { ProfilePic = profilePic; } public String getEmail() { return Email; } public void setEmail(String email) { Email = email; } public String getFirstName() { return FirstName; } public void setFirstName(String firstName) { FirstName = firstName; } public String getServiceUserId() { return ServiceUserId; } public void setServiceUserId(String serviceUserId) { ServiceUserId = serviceUserId; } @Override public boolean equals(Object o) { return o instanceof User && ((User) o).ServiceUserId == ServiceUserId; } } 

还要按照我插入的方式检查下面的代码:

 final User u = new User(); u.setFirstName(mName); u.setEmail(mEmail); u.setProfilePic(mUrl); mUserTable = mClient.getTable(User.class); // Insert the new item new AsyncTask(){ @Override protected Void doInBackground(Void... params) { try { final User entity = mUserTable.insert(u).get(); } catch (Exception e){ //createAndShowDialog(e, "Error"); System.out.println("Error: "+e.toString()); } return null; } }.execute(); 

请帮帮我。

Azure移动服务将自动填充“_createdat”列,因此无需将其包含在模型中。 从User类中删除此属性。 它的存在可能会用null覆盖自动填充的值。

您可以通过从azure中的user表中删除createdAt列来解决此问题。

为什么会出现此错误:

我不确定但我猜这个错误即将发生,因为createdAt是一个不可为空的成员,你不能把它留空。

编辑:

系统列的另一个方面是它们不能由客户端发送。 对于新表(即具有字符串ID的表),如果更新请求的插入包含以“__”(两个下划线字符)开头的属性,则该请求将被拒绝。 但是,’__ createAt’属性可以在服务器脚本中设置(尽管如果你真的不希望该列表示对象的创建时间,你可能想要使用另一列) – 这是一种方法(相当奇怪)场景可以完成。 如果您尝试更新’__updatedAt’属性,它将不会失败,但默认情况下该列由SQL触发器更新,因此您对其所做的任何更新都将被覆盖。

欲了解更多信息,请访问: http : //blogs.msdn.com/b/carlosfigueira/archive/2013/11/23/new-tables-in-azure-mobile-services-string-id-system-properties -和乐观,concurrency.aspx