Entity Framework - Azure Table 存储提供商 - 枚举支持

Entity Framework - Azure Table Storage Provider - Enum Support

我实际上正在为 EF (EntityFramework.AzureTableStorage 7.0.0-beta1) 使用 Azure 存储 Table 提供程序。

我已经了解了如何配置 DbContext:

public class Subscription
{
    public string Environment { get; set; }
        
    public string Name { get; set; }

    ...
    ...            
}

public class EF7Context : DbContext
{
    public DbSet<Subscription> Subscriptions { get; set; }
    
    protected override void OnConfiguring(DbContextOptions options)
    {
        options.UseAzureTableStorage("MyconnectionString");
    }

    protected override void OnModelCreating(Microsoft.Data.Entity.Metadata.ModelBuilder modelBuilder)
    {
        // Configure the Azure Table Storage
        modelBuilder.Entity<Subscription>()
            .ForAzureTableStorage() // Data are stored in an Azure Table Storage.
            .Table("SubscriptionDev") // Name of the Table in the Azure Storage Account
            .PartitionAndRowKey(s => s.Environment, s => s.Name); // Map the partition and the row key
    }
}

但现在我想添加一个枚举作为 Subscription 模型的一部分。 我找到了解决方法:

我有一个枚举:

public enum QueuePriority
{
    High,
    Low
}

我已将这些属性添加到 Subscription class:

public int PriorityId { get; set; }

public QueuePriority Priority
{
    get { return (QueuePriority)PriorityId; }
    set { PriorityId = (int)value; }
}

并在 EF 配置中将 Priority 属性 声明为影子,这样我就不会将 PriorityId 和 Priority 同时存储在 Azure Table 中:

protected override void OnModelCreating(Microsoft.Data.Entity.Metadata.ModelBuilder modelBuilder)
{   
    ...
         
    // We are not mapping the Enum in the database only the IDs.
    modelBuilder.Entity<Subscription>().Property(s => s.Priority).Shadow();
}

所以我想知道是否有更好的方法来完成此操作and/or如果 EF.AzureTableStorage 的下一个版本将支持枚举?

谢谢

您是否看过像 Slazure 这样的动态 ORM,有了它,您将不需要像使用 Entity Framework 那样的设置,并且它支持 Azure Table 存储。为我们节省了大量时间。我不了解它的基本原理,但它会在您编写代码时自动映射所有类型,因此您不需要定义任何实体等。

EF Azure Table Storage beta1 是目前已停产的原型。另见

Azure Table 存储 SDK 版本 > 8.0.0 已经支持枚举和许多其他简单和复杂的 属性 类型,因此您真的不需要为此目的使用额外的框架。

您可以使用 TableEntity.Flatten 方法简单地展平您的 POCO 对象: https://msdn.microsoft.com/en-us/library/azure/mt775435.aspx

将扁平化字典写入table 存储。当您阅读时,只需将 EntityProperties 的字典传递给 TableEntity.ConvertBack 方法: https://msdn.microsoft.com/en-us/library/azure/mt775435.aspx

它会重组您的原始对象,包括其 Enum 和其他类型的属性。

您的实体 classes 不需要从 TableEntity class 继承或实现 ITableEntity 接口,它们可以只是具有简单属性的 POCO 对象,或者它们可能有多层对象图嵌套了自己的复杂属性。 FlattenConvertBack 方法都支持。