[英]Entity Framework + Boolean Matrix
我的目标是在数据库中保存一个正方形(即nxn)的BOOLEAN矩阵,我可以使用实体框架进行查询和修改。 我创建了一个类来对矩阵的每一行建模,称为MatrixRow。 矩阵包含布尔值,我希望能够选择一行,在该行中搜索并找到正确的列,然后返回一个列表/数组/向量/(最有效的方法),其中包含保持该值正确的列号。
我的矩阵行类包含n个布尔变量。 当我在表中查询整行时,我无法遍历变量,因为它们是分开的。 我仍然对C#/ ASP.net / Entity还是陌生的,无法弄清楚如何使数组成为类的成员变量,或者甚至不可能。 我不知道的另一种方法是以某种方式查询数据库中所选行的每一列。 我认为,任何一种解决方案都行得通。
这是我的MatrixRow实体类
public class MatrixRow
{
[Key]
public int AttributeID { get; set; }
//public bool?[] columns { get; set }; //Doesn't work
public bool A1 { get; set; }
public bool A2 { get; set; }
public bool A3 { get; set; }
public bool A4 { get; set; }
public bool A5 { get; set; }
public bool A6 { get; set; }
public bool A7 { get; set; }
public bool A8 { get; set; }
public bool A9 { get; set; }
public bool A10 { get; set; }
public bool A11 { get; set; }
public bool A12 { get; set; }
public bool A13 { get; set; }
public bool A14 { get; set; }
public bool A15 { get; set; }
public bool A16 { get; set; }
public bool A17 { get; set; }
public bool A18 { get; set; }
public bool A19 { get; set; }
public bool A20 { get; set; }
public bool A21 { get; set; }
public bool A22 { get; set; }
public bool A23 { get; set; }
public bool A24 { get; set; }
public bool A25 { get; set; }
public bool A26 { get; set; }
public bool A27 { get; set; }
public bool A28 { get; set; }
public bool A29 { get; set; }
public bool A30 { get; set; }
public bool A31 { get; set; }
public bool A32 { get; set; }
public bool A33 { get; set; }
}
到目前为止,这是我如何使用它的地方。
public void Build()
{
AttributeContext _db = new AttributeContext();
using (VirusDescriptionActions usersShoppingCart = new VirusDescriptionActions())
{
String cartId = usersShoppingCart.GetVirusId();
VirusDescriptionActions.VirusDescriptionUpdates[] cartUpdates = new VirusDescriptionActions.VirusDescriptionUpdates[DescriptionList.Rows.Count];
for (int i = 0; i < DescriptionList.Rows.Count; i++)
{
IOrderedDictionary rowValues = new OrderedDictionary();
rowValues = GetValues(DescriptionList.Rows[i]);
cartUpdates[i].AttributeId = Convert.ToInt32(rowValues["AttributeID"]);
var Row = _db.MatrixRow.Where(b => b.AttributeID == cartUpdates[i].AttributeId);
}
}
}
编辑
所以我固定了类定义以包括数组
public class MatrixRow
{
[Key]
public int AttributeID { get; set; }
public bool?[] columns { get; set; }
}
但是现在我尝试在数据库初始化器文件中使用它,并且收到“无效的初始化器成员声明器”错误。 我猜这是因为我必须先设置内存量,但我不知道如何。
private static List<MatrixRow> GetMatrixRow()
{
var Rows = new List<MatrixRow> {
new MatrixRow
{
AttributeID = 1,
columns[0] = false,
columns[1] = true,
},
};
}
你的原因
public bool?[] columns { get; set };
不起作用很简单。
需要对此进行更改。
public bool?[] columns { get; set; }
原因是; 被放错了地方。
EF无法为bool [] myField建模。 您有2种不同的方式:
将主表拆分为两个表(或将POCO模型分为2个类),相关表应具有3个字段,即主表ID,ArrayIndex和布尔值。 主表应具有Id以及布尔数组中元素的数量(但这是您的选择)。
您只能使用1个带有字符串字段的表来存储YN或01的流,并且不能映射布尔数组(set和get应该在字符串字段上工作,或者更好的是,在string字段上进行get和set在数组上工作)。 此方法有一些变体(即,在字符串字段中序列化布尔数组)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.