WP8中的Linq更新查询错误

时间:2014-07-31 04:58:19

标签: c# sql linq windows-phone-8

我在本地存储中有一个用于维护购物CART项目的表。它包括下面提到的行,DataContext也在这里:

   [Table]
public class Database
{
     [Column(CanBeNull = false, IsPrimaryKey=true,IsDbGenerated=true)]
      public int ProductUniqueID { get; set; }

     [Column(CanBeNull = false)]
     public int ProductID { get; set; }

    [Column(CanBeNull = false)]
    public string ProductName { get; set; }

    [Column(CanBeNull = false)]
    public int ProductPrice { get; set; }

    [Column(CanBeNull = false)]
    public int ProductQuantity { get; set; }

    [Column(CanBeNull = false)]
    public int ProductTotalPrice { get; set; }

    [Column(CanBeNull = true)]
    public string ProductImageURL { get; set; }

    [Column(CanBeNull = true)]
    public string ProductCatID { get; set; }
}

public class MFTDataContext : System.Data.Linq.DataContext
{
    public MFTDataContext(string connectionString)
        : base(connectionString)
    {
    }
    public System.Data.Linq.Table<Database> MFTCart
    {
        get
        {
            return this.GetTable<Database>();
        }
    }

}


现在我在查询中运行ITEM QUANTITY更新以更新项目数量。但它显示错误。

        private const string strConnectionString = @"isostore:/MFTDB9.sdf";

 using (MFTDataContext MFTdb = new MFTDataContext(strConnectionString))
            {
                var itemToChange = (from item in MFTdb.MFTCart
                                    where item.ProductID == singletonInstance.CartItemIDs[singletonInstance.ItemToChange]
                                    select item).Single();


                itemToChange.ProductQuantity = singletonInstance.QuantityChanged;
                itemToChange.ProductTotalPrice = singletonInstance.QuantityChanged * itemToChange.ProductPrice;
                MFTdb.SubmitChanges();

                }

但它给了我错误: System.Data.Linq.ni.dll中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理

1 个答案:

答案 0 :(得分:0)

试试这个:

var itemToChange = from item in MFTdb.MFTCart where item.ProductID == singletonInstance.CartItemIDs[singletonInstance.ItemToChange] select item;
Database record = itemToChange.FirstOrDefault<Database>();
record.ProductQuantity = singletonInstance.QuantityChanged;
record.ProductTotalPrice = singletonInstance.QuantityChanged * record.ProductPrice;
MFTdb.SubmitChanges();