Entity Framework - Azure Table Storage Provider - Enum Support

泪湿孤枕 提交于 2020-01-24 13:19:44

问题


I am actually using the Azure Storage Table provider for EF (EntityFramework.AzureTableStorage 7.0.0-beta1).

I've ended up how to configure the 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
    }
}

But Now I would like to add an enum as part of the Subscription model. I've found a workaround to do this:

I've got an enum :

public enum QueuePriority
{
    High,
    Low
}

I've added these properties to the Subscription class:

public int PriorityId { get; set; }

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

And Declare the Priority property as shadow in EF configuration so that I'm not going to have the PriorityId and the Priority stored both in the 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();
}

So I am wondering if there is a better way to accomplish this and/or If next versions of EF.AzureTableStorage is going to support Enum ?

Thanks


回答1:


EF Azure Table Storage beta1 was a prototype that has been discontinued for now. See also https://stackoverflow.com/a/35071077/2526265




回答2:


Azure Table Storage SDK version > 8.0.0 already supports enums and many other simple and complex property types so you do not really need to use an extra framework just for that purpose.

You can simply flatten your POCO object using TableEntity.Flatten method: https://msdn.microsoft.com/en-us/library/azure/mt775435.aspx

Write the flattened dictionary to table storage. When you read just pass the dictionary of EntityProperties to TableEntity.ConvertBack method: https://msdn.microsoft.com/en-us/library/azure/mt775435.aspx

and it will recompose your original object including its Enum and other types of properties.

Your entity classes do not need to inherit from TableEntity class or implement ITableEntity interface, they can be just POCO objects with simple properties OR they may have nested complex properties of their own with many layers of object graph. Flatten and ConvertBack methods support both.




回答3:


Have you looked at a dynamic ORM like Slazure, With it you won't need this kind of setup like you do with Entity Framework and it supports Azure Table Storage. Saved us a ton of time. I don't understand the fundamentals of it but it automatically maps all your types as you write your code so you don't need to define any entities and such.



来源:https://stackoverflow.com/questions/35142541/entity-framework-azure-table-storage-provider-enum-support

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!