我正在寻找一种解决方案来将不可变数据存储在我的代码而不是数据库中。在具体情况下,我想处理单位。这是重量单位的示例(它们不会改变,所以可以将它们存储在我的代码中):
public class Unit
{
public Unit() { }
public Unit(string name, string symbol, double factor, Unit baseUnit, UnitType unitType)
{
this.Name = name;
this.Symbol = symbol;
this.Factor = factor;
this.BaseUnit = baseUnit;
this.UnitType = unitType;
}
public int Id { get; set; }
public string Name { get; set; }
public UnitType UnitType { get; set; }
public string Symbol { get; set; }
public string NamePlural { get; set; }
public Unit BaseUnit { get; set; }
public double Factor { get; set; }
}
public static class TimeUnits
{
public static Unit second = new Unit("second", "s", 1, null, UnitTypes.Time);
public static Unit microsecond = new Unit("microsecond", "μs", 0.000001, second, UnitTypes.Time);
public static Unit millisecond = new Unit("millisecond", "ms", 0.001, second, UnitTypes.Time);
public static Unit minute = new Unit("minute", "min", 60.0, second, UnitTypes.Time);
public static Unit hour = new Unit("hour", "h", 3600.0, second, UnitTypes.Time);
public static Unit day = new Unit("day", "d", 24.0, hour, UnitTypes.Time);
public static Unit week = new Unit("week", "w", 7, day, UnitTypes.Time);
}
如前所述,我不想将这些不可变信息存储在数据库中,以避免 ef-core 在从数据库检索数据时执行额外的联合。
对于性别,我只需使用枚举:
public enum Gender
{
male = 1,
female = 2,
not_applicable = 9,
dont_want_to_share = 10
}
我想为单位提供类似的解决方案。但枚举只有一个 ID 和一个名称。对于上面显示的单位或其他情况,我需要额外的属性(例如,factore、unitType 等)。非常感谢您提供有关如何实现此目标的任何提示,以便 ef core 像使用枚举一样加载这些值。
烙印99
繁华开满天机
相关分类