62 lines
1.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using LMS.Common.Enum;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace LMS.Repository.DB;
public class Options
{
[Key]
public required string Key { get; set; } = string.Empty;
/// <summary>
/// Value of the option这个值是一个json字符串
/// </summary>
public string? Value { get; set; } = string.Empty;
public OptionTypeEnum Type { get; set; } = OptionTypeEnum.String;
// 写一个字段映射Value判断是不是json字符串是的话就解析成对象
[NotMapped]
public object? ValueObject
{
get
{
if (string.IsNullOrEmpty(Value))
{
return Value;
}
if (Type == OptionTypeEnum.JSON)
{
return JsonConvert.DeserializeObject(Value ?? "{}");
}
if (Type == OptionTypeEnum.Number)
{
return Convert.ToDouble(Value);
}
return Value;
}
set
{
if (value == null)
{
Value = string.Empty;
return;
}
if (Type == OptionTypeEnum.JSON)
{
Value = JsonConvert.SerializeObject(value);
}
else
{
Value = value.ToString();
}
}
}
}