实体框架+布尔矩阵

时间:2015-06-22 00:26:40

标签: c# asp.net entity-framework

我的目标是在数据库中保存一个正方形(即nxn)BOOLEAN矩阵,我可以使用实体框架进行查询和修改。我创建了一个类来模拟矩阵的每一行,称为MatrixRow。矩阵保存布尔值,我希望能够选择一行,搜索该行并查找哪些列为真,并返回包含保持值为true的列号的列表/数组/向量/(无论哪种效果最好) 。

我的矩阵行类包含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; }
}

但是现在我尝试在我的数据库intializer文件中使用它,我得到一个'无效的初始化成员声明符'错误。我猜这是因为我必须首先设置内存量,但我不知道如何。

private static List<MatrixRow> GetMatrixRow()
    {
        var Rows = new List<MatrixRow> {
            new MatrixRow
            {
                AttributeID = 1,
                columns[0] = false,
                columns[1] = true,
            },
         };
     }

2 个答案:

答案 0 :(得分:1)

你的原因

public bool?[] columns { get; set };

不起作用很简单。

需要改为此。

 public bool?[] columns { get; set; }

原因是;错了。

答案 1 :(得分:0)

EF无法模拟bool [] myField。您有两种不同的方式:

  1. 将主表拆分为两个表(或2个类中的POCO模型),相关表应该有3个字段,主表id,arrayindex和boolean值。主表应该具有Id,可能还有布尔数组中的元素数量(但这是您的选择)。

  2. 您只能使用带有字符串字段的1个表来存储YN或01的流,而不是映射布尔数组(set和get应该在字符串字段上工作,或者更好,获取并设置在字符串字段适用于数组)。此方法有一些变体(即在字符串字段中序列化布尔数组)。

相关问题