【问题标题】:Table doesn't have primary key (MissingPrimaryKeyException)表没有主键 (MissingPrimaryKeyException)
【发布时间】:2013-07-27 20:20:58
【问题描述】:

我正在处理购物车,在使用 Find() 方法时遇到了这个 MissingPrimaryKeyException(表没有主键),当我已经为数据表设置了主键时,我很困惑出了什么问题。

我的创建购物车并添加到购物车的代码:

public static void CreateShopCart()
{
        // create a Data Table object to store shopping cart data
        DataTable shoppingCartDataTable = new DataTable("Cart");

        shoppingCartDataTable.Columns.Add("ProductID", typeof(int));
        // make ProductID primary key
        DataColumn[] primaryKeys = new DataColumn[1];
        primaryKeys[0] = shoppingCartDataTable.Columns[0];
        shoppingCartDataTable.PrimaryKey = primaryKeys;

        shoppingCartDataTable.Columns.Add("Quantity", typeof(int));
        shoppingCartDataTable.Columns.Add("UnitPrice", typeof(decimal));
        shoppingCartDataTable.Columns.Add("ProductName", typeof(string));
        shoppingCartDataTable.Columns.Add("ProductDescription", typeof(string));
        shoppingCartDataTable.Columns.Add("SellerUsername", typeof(string));
        shoppingCartDataTable.Columns.Add("Picture", typeof(string));
        // store Data Table in Session
        HttpContext.Current.Session["Cart"] = shoppingCartDataTable;
}

public static void AddShopCartItem(int ProductID, decimal Price, string strPName, string strPDesc, string strSellerUsername, string strImage)
{
    int intQty = 1;
    var retStatus  = HttpContext.Current.Session["Cart"];
    if (retStatus == null)
        CreateShopCart();

    // get shopping data from Session
    DataTable shoppingCartDataTable = (DataTable)HttpContext.Current.Session["Cart"];

    //  Find if ProductID already exists in Shopping Cart

    DataRow dr1 = shoppingCartDataTable.Rows.Find(ProductID); **<- This is the line giving the error** 
    if (dr1 != null)
    {
        // ProductID exists. Add quantity to cart 
        intQty = (int)dr1["Quantity"];
        intQty += 1; // increment 1 unit to be ordered
        dr1["Quantity"] = intQty; // store back into session
    }
    else
    {
        // ProductID does not exist; create a new record
        DataRow dr = shoppingCartDataTable.NewRow();
        dr["ProductID"] = ProductID;
        dr["ProductName"] = strPName;
        dr["ProductDescription"] = strPDesc;
        dr["Quantity"] = intQty;
        dr["UnitPrice"] = Price;
        dr["SellerUsername"] = strSellerUsername;
        dr["Picture"] = strImage;
        shoppingCartDataTable.Rows.Add(dr);
    }

    // store back shopping cart in session
    HttpContext.Current.Session["Cart"] = shoppingCartDataTable;
}

【问题讨论】:

    标签: c#


    【解决方案1】:

    您已使用&lt;ProductID&gt; 将要添加为主键的列命名为ProductID。奇怪的是,这种语法没有错误(至少用 LinqPAD 测试你的代码),但是如果你在添加后尝试打印 PrimaryKey,你会看到没有定义 PrimaryKey。

    所以,这段代码

    DataColumn[] primaryKeys = new DataColumn[1];
    primaryKeys[0] = shoppingCartDataTable.Columns["<ProductID>"];
    shoppingCartDataTable.PrimaryKey = primaryKeys;
    foreach(DataColumn dc in shoppingCartDataTable.PrimaryKey)
        Console.WriteLine(dc.ColumnName);
    

    不产生任何输出

    修复简单地添加 PrimaryKey 的问题

     DataColumn[] primaryKeys = new DataColumn[1];
     primaryKeys[0] = shoppingCartDataTable.Columns["ProductID"];
     shoppingCartDataTable.PrimaryKey = primaryKeys;
     foreach(DataColumn dc in shoppingCartDataTable.PrimaryKey)
         Console.WriteLine(dc.ColumnName);
    

    打印列名

    【讨论】:

    • 对不起,我复制了错误的编码。它应该是 primaryKeys[0] = shoppingCartDataTable.Columns[0];我试过你的方法,可惜还是不行。
    • 您能否尝试调试您的代码并检查(在将表添加到会话之前)是否正确定义了主键?并且,如果从 Session 检索表后 PrimaryKey 仍然存在?我已尝试使用您的代码的简化版本(不涉及会话)并且没有发生任何奇怪的事情。
    【解决方案2】:

    我琢磨了一整天,发现我犯了一个错误,我没有调用 CreateShopCart 方法,这就是为什么 AddShopCartItem 方法在表中没有主键的原因。

    干杯,感谢您的帮助。

    【讨论】:

      猜你喜欢
      • 2011-04-03
      • 2019-05-23
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 2012-02-14
      • 2013-05-09
      • 1970-01-01
      相关资源
      最近更新 更多