我需要像这样存储一个 bool 矩阵:
public bool[][] Schedule { get; set; }
它在 View 中有效,但我无法使用 Entity Framework 正确存储它,因为它甚至不会在迁移时创建表。我该如何存储它?
最佳答案
就此信息在数据库中的实际存储而言,可以采用如下格式:
类型数据库表(可选,使用枚举)
Id Type
1 Segunda
2 Terca
3 Quarta
4 Sexta
值数据库表
Id TimeFrom TimeTo TypeId Selected
1 8:00 9:00 1 1
2 8:00 9:00 2 1
3 8:00 9:00 3 1
4 8:00 9:00 4 1
5 8:00 9:00 5 1
6 9:00 10:00 1 0
7 9:00 10:00 2 0
8 9:00 10:00 3 1
9 9:00 10:00 4 0
10 9:00 10:00 5 0
11 10:00 11:00 1 1
12 10:00 11:00 2 1
13 10:00 11:00 3 1
14 10:00 11:00 4 1
15 10:00 11:00 5 1
例如,以下域对象将支持这种存储布局:
public enum TypeEnum
{
Segunda = 1,
Terca = 2,
Quarta = 3,
Sexta = 4
}
public class Type
{
public int Id { get; set; }
public TypeEnum Type { get; set; }
}
public class Schedules
{
public int Id { get; set; }
public DateTime TimeFrom { get; set; }
public DateTime TimeTo { get; set; }
public TypeEnum TypeId { get; set; }
public bool Selected { get; set; }
}
如果您要为多个用户存储此信息或只是为了规范化数据,则可以将时间从另一个表推断到另一个表。
应该很容易将这些值映射到合适的 View 模型,以便在 View 中来回映射。
关于c# - 如何在 MVC ASP.NET 中存储 bool 矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34289835/