【问题标题】:Azure Storage Client v4.1 - a value of the non-primitive type was expectedAzure 存储客户端 v4.1 - 需要非原始类型的值
【发布时间】:2014-08-16 17:39:59
【问题描述】:

我最近升级了我的 ASP.NET 项目 (MVC5) 以使用存储库 4.1 来定位 Azure SDK 2.3,当我尝试将任何内容保存到表存储时遇到一个奇怪的错误。

错误:

Microsoft.WindowsAzure.Storage.dll 中出现“Microsoft.WindowsAzure.Storage.StorageException”类型的未处理异常

附加信息:指定了原始值;但是,需要非原始类型 '' 的值。

我的模型通过使用TableServiceContext 来添加、更新、删除和保存的存储库进入表存储。

我的模型遵循这种模式:

[System.Data.Services.Common.DataServiceKey(new string[] { "PartitionKey", "RowKey" })]
public class PersistedAlert : Alert, ITableEntity
{
    public string PartitionKey
    {
        get { return this.StudentId; }
        set { this.StudentId = value; }
    }

    public string RowKey
    {
        get { return this.Id; }
        set { this.Id = value; }
    }

    public DateTime Timestamp { get; set; }

    public new int Type { get; set; } //hides Enum type in Alert base class
}

在升级过程中,我需要换掉所有对

的引用

System.Data.Services.*

Microsoft.Data.Services.*

...除了 OData 库。

内部是否发生了一些变化,使我的模式不再有效?

【问题讨论】:

  • 我遇到了同样的错误,预期类型为空,使用 Web API 2 和 OData V4...对于这个错误绝对没有任何帮助。这让我发疯了!

标签: c# asp.net azure azure-table-storage


【解决方案1】:

由于网络上没有(还)这个错误,而且这几乎是唯一讨论它的地方,即使我的上下文与你的不同,我也会添加一个解决方案。错误完全一样,所以我猜它来自同一个地方。

对我来说,是一个继承的主键导致了问题。序列化实体的主键必须是自然的并且不能被覆盖。如果 Class 具有 ID 属性,则 DerivedClass 还必须将 ID 属性声明为“新”,或者必须将 ID 属性从 Class 移动到 DerivedClass。

这里有更多详细信息:http://jerther.blogspot.ca/2014/12/aspnet-odata-v4-primitive-value-was.html

我确实认为这是一个错误而不是限制,因为继承的密钥与 Entity Framework 和 Fluent API 配合得很好。

我希望这会有所帮助并节省一些头发拉扯。

【讨论】:

  • 当我使用 fluent API 将一个字段映射到与另一个字段相同的名称时,这发生在我身上。从我们的两种情况来看,我认为此错误是由映射为相同名称的两个字段引起的。
【解决方案2】:

最后,我决定升级所有存储库代码,以摆脱基于 WCF 的 TableServiceContext 已弃用,而是通过 CloudTable 进行调用。我只能假设在内部对上述库之一进行了更改,这导致了我看到的问题。

除了更改存储库代码之外,我还需要更新我的实体以从 Azure ITableEntity 继承(我之前有自己的风格),如下所示:

public class PersistedAlert : Alert, Microsoft.WindowsAzure.Storage.Table.ITableEntity
{
        public string PartitionKey
        {
            get { return this.StudentId; }
            set { this.StudentId = value; }
        }

        public string RowKey
        {
            get { return this.Id; }
            set { this.Id = value; }
        }

        public DateTimeOffset Timestamp { get; set; }

        public string ETag { get; set; }

        public void ReadEntity(IDictionary<string, Microsoft.WindowsAzure.Storage.Table.EntityProperty> properties, Microsoft.WindowsAzure.Storage.OperationContext operationContext)
        {
            Microsoft.WindowsAzure.Storage.Table.TableEntity.ReadUserObject(this, properties, operationContext);
        }

        public IDictionary<string, Microsoft.WindowsAzure.Storage.Table.EntityProperty> WriteEntity(Microsoft.WindowsAzure.Storage.OperationContext operationContext)
        {
            return Microsoft.WindowsAzure.Storage.Table.TableEntity.WriteUserObject(this, operationContext);
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    • 2018-04-25
    • 2022-12-15
    • 2021-06-21
    • 1970-01-01
    • 2018-06-24
    相关资源
    最近更新 更多