I am trying to control the serialization of Azure table objects as a continuation. How do I get multiple types of objects using a single query in Azure Table Storage? and encountered unexpected behavior (for me).
When retrieving elements, the name is always " <myaccountname>.Pets" no matter what I set when I save the object. Why type name is not saved.
According to MSDN:
DataServiceContext.ResolveType
Gets or sets the function that is used to override the default permission setting used by the client library when receiving objects from the Open Data Protocol (OData) service.
DataServiceContext.ResolveName
Gets or sets a function to override the default permission strategy used by the client library when sending entities to the data service.
And this blog post should not be.
Here is a simple test:
public class Pet : TableServiceEntity { }
public class Cat : Pet { }
public class Dog : Pet { }
public class Test
{
public void RunTest()
{
this.Read();
}
public TableServiceContext GetTableServiceContext()
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
tableClient.CreateTableIfNotExist("Pets");
TableServiceContext serviceContext = tableClient.GetDataServiceContext();
serviceContext.ResolveName = (entityType) =>
{
return entityType.FullName;
};
serviceContext.ResolveType = (s) =>
{
return Type.GetType(s);
};
return serviceContext;
}
public void Create()
{
var serviceContext = this.GetTableServiceContext();
var cat = new Cat() { PartitionKey = "cats", RowKey = "1" };
serviceContext.AddObject("Pets", cat);
var dog = new Dog() { PartitionKey = "dogs", RowKey = "1" };
serviceContext.AddObject("Pets", dog);
serviceContext.SaveChangesWithRetries();
}
public void Read()
{
var serviceContext = this.GetTableServiceContext();
var pets = serviceContext.CreateQuery<Pet>("Pets").AsTableServiceQuery<Pet>().ToArray();
foreach (var pet in pets)
{
Console.WriteLine(pet.GetType());
}
}
}
source
share