starcorn
starcorn

Reputation: 8531

Cannot save to Azure table

I get DataServiceRequestException exception when I try to save an entity to azure table. It happens at this line _myContext.SaveChangesWithRetries(); I've tried to google to see what the problem could be. But could not find any answer to it.

Anyone kow what the problem could be?

Storage creator

public CloudTableClient GetMusicClient() {
      //retrieve connection string from settings
      CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
          RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));

      //create table client
      CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

      //create table if doesn't exist
      string tableName = "music";
      tableClient.CreateTableIfNotExist(tableName);

      return tableClient;
}

WebAPI

public Music PostMusic(string genre, string artist, string random)
{
    CloudTableClient _myTableClient = _myTableRepo.GetMusicClient();
    TableServiceContext _myContext =  _myTableClient.GetDataServiceContext();

    Music music = new Music(genre, artist);
    music.Score = "10";
    music.Year = "2012";
    music.Random = random;
    try
    {
        _myContext.AddObject(random, music);
        _myContext.SaveChangesWithRetries();
    }
    catch (StorageClientException e)
    { }
    catch (DataServiceRequestException e)
    {
        System.Diagnostics.Debug.WriteLine(e.StackTrace);
    }

    return music;

}

stacktrace

An error occurred while processing this request.
   at Microsoft.WindowsAzure.StorageClient.Tasks.Task`1.get_Result()
   at Microsoft.WindowsAzure.StorageClient.Tasks.Task`1.ExecuteAndWait()
   at MyWebAPI.Controllers.ValuesController.PostMusic(String genre, String artist, String random) in C:\MyWebAPI\MyWebAPI\Controllers\ValuesController.cs:line 61

Music.cs

public class Music : TableServiceEntity
    {
        public Music(string genre, string artist)
        {
            this.PartitionKey = genre;
            this.RowKey = artist;
        }
        public Music() {}
        public string Score { get; set; }
        public string Year { get; set; }
        public string Random { get; set; }
    }

Upvotes: 1

Views: 347

Answers (1)

user94559
user94559

Reputation: 60143

Oh, it looks like your first argument to AddObject is wrong? It should be "music," the name of your table, but if the parameter name is to be believed, it looks like you're passing in a random string.

Upvotes: 1

Related Questions