Azure Table 存储 - 尝试获取实体 returns 错误消息 CS0311
Azure Table storage - Trying to GetEntity returns error message 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
这是旧的 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; }
}
这是新的:
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; }
}
感谢 Gaurav Mantri and dot 发布您的讨论作为帮助其他社区成员的答案。
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; }
}
我有以下方法:
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
这是旧的 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; }
}
这是新的:
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; }
}
感谢 Gaurav Mantri and dot 发布您的讨论作为帮助其他社区成员的答案。
add
ITableEntity
interface inWidget
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; }
}