c# - 仅存储年份和月份的实体框架类型
问题描述
我想在实体上保存生产年份和月份。我不想保存月份的时间和日期。
我可以想出多种解决方案,目前最好的两个是:
public class Product
{
private DateTime _productionMonth;
public DateTime ProductionMonth
{
get => _productionMonth;
set => _productionMonth = new DateTime(value.Year, value.Month, 1);
}
}
或者
public class Product
{
public int Year { get; set; }
public int Month { get; set; }
}
但我不觉得任何解决方案都是完美的解决方案......
有没有更好的解决方案?也许是一个属性DateTime
?
解决方案
您可以使用 YYYYMM 格式将其存储为单个整数。实体可以公开未映射 ( [NotMapped]
) 辅助方法以覆盖 to/fromDateTime
和/或 ValueTuple,(year int, month int)
尽管您需要使用单个映射值从 EF 查询。YYYYMM 是可排序的,因此您可以有效地查询范围。
推荐阅读
- xslt-2.0 - 对组中的所有单个 emenets 应用第二个分组
- javascript - 按 Google Doc 中的字数检查用户贡献
- php - 我需要计算数组值
- python - Snakemake 仅将输出中的第一个路径传递给 shell 命令
- postgresql - 如果列包含postgre中的特定值,如何从子查询中选择
- java - 如何从java中的字符串中删除前导零
- java - 如何使用规范在mysql查询中使用多个“AND”条件创建两个“OR”条件
- python - python中与多条线最近的交点
- css - 动态 LESS 混合
- python-3.x - 试图从 dict.items() 返回值不起作用