Azure 表存储 - 尝试 GetEntity 返回错误消息 CS0311
我有以下方法:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)