本文介绍了我可以将枚举作为字符串存储在EF 5中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在解决方案中使用EF CF已有一段时间了。大粉丝!到目前为止,我们一直在使用黑客来支持枚举(在模型上创建一个额外的字段;忽略枚举期间的映射;将多余的字段映射到我们将使用的db中的列)。传统上,我们将枚举作为字符串(varchars)存储在数据库中(使其美观且可读)。现在有了EF 5(Beta 2)中的枚举支持,它似乎只支持将枚举映射到数据库中的int列...。我们可以让EF 5将枚举存储为它们的字符串表示形式。

We have been using EF CF for a while in our solution. Big fans! Up to this point, we've been using a hack to support enums (creating an extra field on the model; ignore the enum durring mapping; and map the extra field to the column in the db that we would have used). Traditionally we have been storing our enums as strings(varchars) in the DB (makes it nice and readable). Now with enum support in EF 5 (Beta 2) it looks like it only supports mapping enums to int columns in the DB....Can we get EF 5 to store our enums as their string representation.

类型是DocumentType类型的枚举

Where "Type" is an enum of type DocumentType

public enum DocumentType
    {
        POInvoice,
        NonPOInvoice,
        Any
    }

我尝试使用以下方式映射它:

I tried to map it using:

public class WorkflowMap : EntityTypeConfiguration<Model.Workflow.Workflow>
    {
        public WorkflowMap()
        {
            ToTable("Workflow", "Workflow");
            ...
            Property(wf => wf.Type).HasColumnType("varchar");

        }
    }

我以为是魔术子弹,但是..

I thought was going to be the magic bullet but..

这只是抛出:

您的想法吗?

推荐答案

目前这是不可能的。 EF中的枚举与CLR中的枚举具有相同的限制-它们只是命名为整数值集。查看进行确认:

This is currently not possible. Enum in EF has same limitations as enums in CLR - they are just named set of integer values. Check this article for confirmation:

我发布了和问题。如果您以后希望使用此功能,请对建议进行投票。

I posted article and related suggestion about this problem. If you want to see this feature in the future please vote for the suggestion.

这篇关于我可以将枚举作为字符串存储在EF 5中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 16:01