Azure 表存储 - 尝试 GetEntity 返回错误消息 CS0311

发布于 2025-01-09 14:34:12 字数 2879 浏览 0 评论 0原文

我有以下方法:

   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;
        }
    }

我在调用 GetEntity() 的行上收到错误。错误是:

类型“MyProject.Models.Widget”不能用作通用类型或方法“TableClient.GetEntity(string, string, IEnumerable, CancellationToken)”中的类型参数“T”。没有从“MyProject.Models.Widget”到“Azure.Data.Tables.ITableEntity”的隐式引用转换。csharp(CS0311)

我正在尝试做这样的事情:(伪代码)

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

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

我看不到我在哪里出了问题。

编辑 1

这是旧类,

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; }
  }

这是新类:

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; }
  }

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; }
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凯凯我们等你回来 2025-01-16 14:34:12

Widget类中添加ITableEntity接口来解决问题

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; }
}

addITableEntity 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; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文