代码之家  ›  专栏  ›  技术社区  ›  Larry OBrien

如何通过实现IDataServiceUpdateProvider来更新Azure表存储资源?

  •  0
  • Larry OBrien  · 技术社区  · 14 年前

    我可以添加和删除对象,但当我尝试更新资源时,SaveChanges()似乎不会“拾取”在对SetProperty的调用中分配的值。

    //Works fine
    public object GetResource(IQueryable query, string fullTypeName)
        {
            var resource = query.Cast<MyDataModel>().SingleOrDefault();
            if (fullTypeName != null && resource.GetType().FullName != fullTypeName)
            {
                throw new ApplicationException("Unexpected type for this resource");
            }
            return resource;
        }
    
    //Seems to work fine: gets called for each property.
    public void SetValue(object targetResource, string propertyName, object propertyValue)
        {
            var propInfo = targetResource.GetType().GetProperty(propertyName);
            propInfo.SetValue(targetResource, propertyValue, null);
        }
    
    //This gets called, but resource is not updated 
    void IUpdatable.SaveChanges()
        {
            //Forwarding from IUpdatable.SaveChanges() to DataServiceContext.SaveChanges()
            base.SaveChanges();
        }
    

    更新:答案是在SetValue()期间调用UpdateObject():

    public void SetValue(object targetResource, string propertyName, object propertyValue)
        {
            var propInfo = targetResource.GetType().GetProperty(propertyName);
            propInfo.SetValue(targetResource, propertyValue, null);
            UpdateObject(targetResource);
        }
    
    1 回复  |  直到 14 年前
        1
  •  0
  •   Larry OBrien    14 年前

    更新:答案是在SetValue()期间调用UpdateObject():

    { propInfo.SetValue(targetResource,propertyValue,空); 更新对象(targetResource); }