gpt4 book ai didi

c# - 如何将数据库中的枚举保存为字符串

转载 作者:IT王子 更新时间:2023-10-29 04:30:19 27 4
gpt4 key购买 nike

这是我的模型类,我们有一个类型,可以是僵尸或人类。

public class User
{
public int ID { get; set; }
public string Name { get; set; }
public Type Type { get; set; }
public List<Weapon> WeaponsInList { get; set; }
}

public enum Type
{
[Description("Zombie")]
Zombie,

[Description("Human")]
Human
}

目前正在Int中保存数据。

enter image description here

我想将数据保存为 Human 和 Zombie,而不是 int。

最佳答案

在 Entity Framework Core 中,您可以指定内置转换:

modelBuilder
.Entity<DataSet>()
.Property(d => d.SemanticType)
.HasConversion(new EnumToStringConverter<DataSetSemanticType>());

更多详情 here .

关于c# - 如何将数据库中的枚举保存为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32542356/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com