简体   繁体   English

将 EF Core 3.1 转换为存储字符串枚举值,而不是整数

[英]Transition EF Core 3.1 to storing string enum values, rather than ints

I'd like to know if there is an elegant solution to the problem of an existing database that stores enum values as ints, but wanting to transition to storing strings for new records, but leave the ints alone in old records.我想知道对于将枚举值存储为整数的现有数据库的问题是否有一个优雅的解决方案,但想要过渡到存储新记录的字符串,但将整数单独留在旧记录中。 Naturally, we can make the column types from int to string, and "2" meaning "Ready" can still be stored in a string column..自然地,我们可以将列类型从 int 变为 string,而表示“Ready”的“2”仍然可以存储在 string 列中。

Is there a technique that allows EF to parse either "2" or "Ready" to that Status.Ready in enum Status { Ready = 2 } ?是否有一种技术允许 EF 在enum Status { Ready = 2 }中将“2”或“Ready”解析为该Status.Ready I know that EF Core has an EnumStringConverter that can understand "Ready" -> Status.Ready but is there a way of extending or customizing it so that it tries to parse the db value both ways?我知道 EF Core 有一个 EnumStringConverter 可以理解"Ready" -> Status.Ready但是有没有办法扩展或自定义它,以便它尝试双向解析 db 值? ("Ready" as enum element name -> success, "2" as enum element name -> fail, "2" as enum element value -> success) Would we write a conversion process that eg examined if all the characters in the db value were numeric and hence parse it as a value, else parse it as a name? (“Ready”作为枚举元素名称 -> 成功,“2”作为枚举元素名称 -> 失败,“2”作为枚举元素值 -> 成功)我们是否会编写一个转换过程,例如检查数据库中的所有字符值是数字,因此将其解析为值,否则将其解析为名称?

Or do people who switch from using ints to using strings also write a massive migration to UPDATE every value in every enum'd column so that all the existing "2" become "Ready"?或者从使用整数切换到使用字符串的人是否也编写了大量迁移来更新每个枚举列中的每个值,以便所有现有的“2”都变成“就绪”?

You could use a custom converter , like the following您可以使用自定义转换器,如下所示

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder
        .Entity<MyEntity>()
        .Property(e => e.MyStatusColumn)
        .HasConversion(
            v => v.ToString(),
            v => int.TryParse(v, out int val) ? 
                 (Status)val : 
                 (Status)Enum.Parse(typeof(Status), v)
        );
}

From my own practice, I store enum values as int in the database.根据我自己的实践,我将枚举值作为 int 存储在数据库中。 For presentation purposes in the client side, I do the following trick.为了在客户端进行演示,我做了以下技巧。

In the entity class that has your enum property, Status, create another property that gets string value of that enum:在具有枚举属性 Status 的实体 class 中,创建另一个获取该枚举字符串值的属性:

 public enum Status
 {
    None,
    Ready,
    InProcess,
    NotReady
 }

 public class Process
 {
    public int Id { get; set; }
    public Status Status { get; set; }
    public string GetStatus 
    {
        get
        {
            return Status.ToString();
        }
    }
 }

Here, if your Process object has 1 as value of the Status property, the GetStatus property just returns Ready string.在这里,如果您的 Process object 的 Status 属性值为 1,则 GetStatus 属性仅返回 Ready 字符串。

Hope, this answers to your question.希望,这回答了你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM