我正在使用Windows Azure表存储工作一个示例应用程序。我将尝试使用一些代码来解释它:
//GetStudent is a service call
StudentDetails student = this.GetStudent(studentID);
此代码返回一个StudentDetails对象,PartitionKey和RowKey都为null,因为这两个对象都不是DataContract中的DataMembers。
//Update the student object
student.LastName = "New Last Name";
this.UpdateStudent(student);//Another service call
我的更新服务代码如下所示:
context.AttachTo(StudentDataServiceContext.studentTableName, student, "*");
context.UpdateObject(student);
context.SaveChangesWithRetries(SaveChangesOptions.ReplaceOnUpdate);
当我运行此代码时,我收到以下错误:
One of the request input is not valid
我找到了解决此问题的解决方法并更新了UpdateService代码,如下所示:
StudentDetails temp = (from c in context.StudentTable
where c.PartitionKey == "Student" && c.RowKey == student.ID
select c).FirstOrDefault();
//Copy each and every property from student object to temp object
temp.LastName = student.LastName;
context.UpdateObject(temp);
context.SaveChangesWithRetries(SaveChangesOptions.ReplaceOnUpdate);
这很好用,对象在表存储中更新。
但有没有更好的方法呢?为什么AttachTo函数不适用于我的情况?
修改
为了让我的问题更清楚,这是我的StudentDetails类:
[DataContract]
public class StudentDetails
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
[DataMember]
public string First Name { get; set; }
[DataMember]
public string Last Name { get; set; }
[DataMember]
public string ID { get; set; }
}
以下是我的GetStudent方法:
BasicHttpBinding myBinding = new BasicHttpBinding();
EndpointAddress myEndpoint = new EndpointAddress(RoleEnvironment.GetConfigurationSettingValue("StudentServiceURI"));
ChannelFactory<IPatientService> myChannelFactory = new ChannelFactory<IStudentService>(myBinding, myEndpoint);
IStudentService proxy = myChannelFactory.CreateChannel();
student = proxy.GetPatient(studentID);
((IClientChannel)proxy).Close();
myChannelFactory.Close();
我觉得问题在于我的GetStudent的频道工厂调用缺少与服务上下文相关的内容。我只是不知道是什么。
答案 0 :(得分:1)
您说返回的Student
对象在返回时没有设置PartitionKey
和RowKey
。然后尝试更新该对象。如果您在调用PartitionKey
之前未自己设置RowKey
和.Update()
,则这将失败,因为基础REST API依赖于这些。
答案 1 :(得分:0)
在开发存储中使用空表测试应用程序时会发生这种情况。这是因为在允许查询之前,开发存储当前要求先前已定义存储在表中的实体的模式。
解决方法
解决方法很简单,如果应用程序在开发结构中运行,我们只需要在Windows Azure表中插入一个虚拟行,然后删除它。在初始化Web角色期间,最好是应用程序检查它是否针对本地开发存储运行,如果是这种情况,它会将虚拟记录添加到应用程序的Windows Azure表中,然后将其删除(在针对开发存储工作时,它将在每个实体上第一次完成。
例如,可以使用扩展方法添加该代码。
更多信息: