Home > Net >  Azure Table storage - Trying to GetEntity returns error message CS0311
Azure Table storage - Trying to GetEntity returns error message CS0311

Time:02-25

I have the following method:

   public async Task<Boolean> MarkAsProvisioned( string requestId)            
    {           
        try
        {
            //populate the connection object: 
             GetStorageAccountConnectionData();
            //lookup the record we need to change by the requestId.  
            var serviceClient = new TableServiceClient(
                new Uri(connection.storageUri),
                new TableSharedKeyCredential(connection.storageAccountName, connection.storageAccountKey));
            var tableClient = serviceClient.GetTableClient(connection.tableName);
            Azure.Response<Widget> response = tableClient.GetEntity<Widget>(
                                "mypartitionKey",
                                requestId);
            Widget entity = response;
            
            return true;

        } 
        catch (Exception ex)
        {
            Console.Write(ex.Message);
            return false;
        }
    }

I'm getting the error on the line where I'm calling GetEntity(). The error is :

The type 'MyProject.Models.Widget' cannot be used as type parameter 'T' in the generic type or method 'TableClient.GetEntity(string, string, IEnumerable, CancellationToken)'. There is no implicit reference conversion from 'MyProject.Models.Widget' to 'Azure.Data.Tables.ITableEntity'.csharp(CS0311)

I'm trying to do something like this: (Pseudocode)

Widget entity = table.GetEntity<Widget >(partitionKey, rowKey);
entity.Name = newMessage;

table.UpdateEntity(entity, ETag.All, TableUpdateMode.Replace);

I can't see where I've gone wrong.

EDIT 1

This was the old class

using Newtonsoft.Json;
using System;

namespace MyProject.Models
{

    public class Widget
  {
    

    [JsonProperty(PropertyName = "requestId")]
     public string requestId { get; set; }

    [JsonProperty(PropertyName = "status")]
    public string status { get; set; }

    [JsonProperty(PropertyName = "request")]
    public WidgetRequest { get; set; }
  }

this is the new:

using Newtonsoft.Json;
using Azure.Data.Tables;
using System;
using Azure;

namespace MyProject.Models
{

    public class Widget:ITableEntity
  {
    public string PartitionKey { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
    public string RowKey { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
    public DateTimeOffset? Timestamp { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
    public ETag ETag { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

    [JsonProperty(PropertyName = "requestId")]
     public string requestId { get; set; }

    [JsonProperty(PropertyName = "status")]
    public string status { get; set; }

    [JsonProperty(PropertyName = "request")]
    public WidgetRequest { get; set; }
  }

CodePudding user response:

thank you Gaurav Mantri and dot posting your discussions as answer to help other community members.

add ITableEntity interface in Widget class to resolve the issue

using Newtonsoft.Json;
using Azure.Data.Tables;
using System;
using Azure;
namespace MyProject.Models
{
public class Widget:ITableEntity
{
public string PartitionKey { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public string RowKey { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public DateTimeOffset? Timestamp { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public ETag ETag { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
[JsonProperty(PropertyName = "requestId")]
public string requestId { get; set; }
[JsonProperty(PropertyName = "status")]
public string status { get; set; }
[JsonProperty(PropertyName = "request")]
public WidgetRequest { get; set; }
}
  • Related