简体   繁体   English

如何从 Json 文件中按名称播种枚举

[英]How can I seed an enum by name from a Json file

I'm seeding data into my database on startup.我在启动时将数据播种到我的数据库中。

Problem - I want to seed the "YogaPose" by name, not number, as my YogaPose enum is really long (~150+) and trying to count the number is taking too long.问题- 我想通过名称而不是数字来播种“YogaPose”,因为我的 YogaPose 枚举真的很长(~150+)并且试图计算数字需要太长时间。

Question - Is this possible?问题- 这可能吗?

Here is what the code looks like to get the json data.这是获取 json 数据的代码。

if (context.Poses.Any())
{
      var posesData =
         File.ReadAllText(path + @"/Data/SeedData/poses.json");

      var poses = JsonSerializer.Deserialize<List<Pose>>(posesData);

      foreach (var pose in poses)
      {
          context.Poses.Add(pose);
      }

      await context.SaveChangesAsync();
}

Here is a sample section from the json file, where you can see I'm using the YogaPose enum number.这是 json 文件中的示例部分,您可以在其中看到我使用的是 YogaPose 枚举编号。 I want to use the name of the enum!我想使用枚举的名称!

Attempt 1 - I tried using "YogaPose": "YogaPose.Crane" with a failure尝试 1 - 我尝试使用 "YogaPose": "YogaPose.Crane" 失败

Attempt 2 - I tried using "YogaPose": "Crane" with a failure尝试 2 - 我尝试使用 "YogaPose": "Crane" 失败

 { "YogaPose": 1, "Description": "A compact arm balance, Crane Pose/Crow Pose, called Bakasana in Sanskrit, encourages toning in the abs and the arms, strengthening in the core, and improves focus in the mind.", "Level": 2, "YogaPoseTypes": [{"YogaType": 1}], "Alias": "Crow", "Icon": "fa fa-users", "PreparatoryPoses": [{"YogaPose": 7},{"YogaPose": 44},{"YogaPose": 14},{"YogaPose": 122}], "FollowUpPoses": [{"YogaPose": 5},{"YogaPose": 7}] }

Here is the Pose entity这是姿势实体

public class Pose : BaseEntity
{
    public YogaPose YogaPose { get; set; }
    public string Description { get; set; }
    public YogaLevel Level { get; set; }
    public ICollection<YogaPoseType> YogaPoseTypes { get; set; }
    public string Sanskrit { get; set; }
    public string Benefit { get; set; }
    public string Alias { get; set; }
    public string Icon { get; set; }
    public ICollection<PreparatoryPose> PreparatoryPoses { get; set; }
    public ICollection<FollowUpPose> FollowUpPoses { get; set; }
}

Here is a small section of the YogaPose enum这是 YogaPose 枚举的一小部分

public enum YogaPose
{
    [Display(Name = "Crane")]
    Crane,
    [Display(Name = "Dolphin")]
    Dolphin
}

Yes, it's possible but depends on a json converter you are using (Newstonsoft or System.Text.Json).是的,这是可能的,但取决于您使用的 json 转换器(Newstonsoft 或 System.Text.Json)。 Please, have a look to the article: JsonConverter attributes .请看一下文章: JsonConverter 属性

You also should prepare another json input file with strings instead of numbers in the field "YogaPose", for example:您还应该准备另一个 json 输入文件,在“YogaPose”字段中使用字符串而不是数字,例如:

{
    "YogaPose": "Crane",
    "Description": "A compact arm balance, Crane Pose/Crow Pose, called Bakasana in Sanskrit, encourages toning in the abs and the arms, strengthening in the core, and improves focus in the mind.",
    "Level": 2,
    "YogaPoseTypes": [{"YogaType": 1}],
    "Alias": "Crow",
    "Icon": "fa fa-users",
    "PreparatoryPoses": [{"YogaPose": 7},{"YogaPose": 44},{"YogaPose": 14},{"YogaPose": 122}],
    "FollowUpPoses": [{"YogaPose": 5},{"YogaPose": 7}]
}

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

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