1

早晨,

我的一些代码有问题......基本上我正在尝试更新或插入数据库。添加新产品时的第一个 if 语句 if for。然后 else 应该更新任何现有产品。

但是,当我运行它时,它不会更新数据库中的现有产品。但是,它正在设置准备更新的项目。有任何想法吗?

非常感谢...

using (aboDataDataContext dc = new aboDataDataContext())
            {
                foreach (abcProduct p in abcProducts)
                {

                    var match = (from t in dc.abcProducts
                                 where t.sku == p.productcode
                                 select t).FirstOrDefault();

                    if (match == null)
                    {

                        // Watch out here; there is some type conversion required for certain fields!
                        abcProduct prod = new abcProduct();

                        prod.sku = p.productcode;
                        prod.categoryId = dc.Categories.Single(c => c.Name == p.category).Id;
                        prod.title = p.name;
                        prod.brand = p.manufacturer;
                        prod.description = p.description;
                        prod.abcPrice = p.price;
                        prod.size = decimal.TryParse(p.size.Replace("cl", ""), out size) == true ? (int?)size : null;
                        prod.country = p.country;
                        prod.region = p.region;
                        prod.vintage = int.TryParse(p.vintage, out vintage) == true ? (int?)vintage : null;
                        prod.weight = Convert.ToDecimal("1.50");
                        prod.strength = p.strength;
                        prod.bottler = p.bottler;
                        prod.age = int.TryParse(p.age, out age) == true ? (int?)age : null;
                        prod.caskType = p.casktype;
                        prod.series = p.series;
                        prod.flavour = p.flavour;
                        if (p.freestock <= 0) { prod.stock = 0; } //check to see if stock is 0
                            else { prod.stock = p.freestock; }
                        prod.abcUpdated = false;
                        prod.stockUpdated = false;
                        prod.priceUpdated = false;
                        prod.pricePublished = false;
                        prod.stockPublished = false;
                        prod.imgPublished = false;
                        prod.prodPublished = false;
                        prod.lastUpdated = DateTime.Now;

                        // Add the new object to the abcProducts table (only in memory here)
                        dc.abcProducts.InsertOnSubmit(prod);
                    }
                    else
                    {
                        // update row
                        match.abcUpdated = true;
                        //Set if an item has been updated or not.
                        if (match.stock == p.freestock) { match.stockUpdated = false; }
                        else { match.stockUpdated = true; }
                        if (match.abcPrice == p.price) { match.priceUpdated = false; }
                        else { match.priceUpdated = true;}
                        match.sku = p.productcode;
                        match.categoryId = dc.Categories.Single(c => c.Name == p.category).Id;
                        match.title = p.name;
                        match.brand = p.manufacturer;
                        match.description = p.description;
                        match.stock = p.freestock;
                        match.abcPrice = p.price;
                        match.size = decimal.TryParse(p.size.Replace("cl", ""), out size) == true ? (int?)size : null;
                        match.weight = Convert.ToDecimal("1.50");
                        match.country = p.country;
                        match.region = p.region;
                        match.vintage = int.TryParse(p.vintage, out vintage) == true ? (int?)vintage : null;
                        match.strength = p.strength;
                        match.bottler = p.bottler;
                        match.age = int.TryParse(p.age, out age) == true ? (int?)age : null;
                        match.caskType = p.casktype;
                        match.series = p.series;
                        match.flavour = p.flavour;
                        if (p.freestock <= 0) { match.stock = 0; } //check to see if stock is 0
                            else { match.stock = p.freestock; }
                        match.abcUpdated = true;
                        match.pricePublished = false;
                        match.stockPublished = false;
                        match.imgPublished = false;
                        match.prodPublished = false;
                        match.lastUpdated = DateTime.Now;
                    }
                }

                // Finally, request Linq to perform the database updates.
                dc.SubmitChanges();
            }
            return null;
        }
4

2 回答 2

4

设置匹配时,上下文会丢失对象的跟踪。

在 else 语句的底部插入

dc.abcProducts.Attach(match);
于 2012-07-06T10:40:01.413 回答
3

您的代码存在问题,您正在遍历产品表并获取match变量中的值。在 if 语句中match 不为 null 的部分,您将dc.SubmitChanges();对象设置为新值,但您没有调用match,它被分配了新的值。

您需要调用 dc.SubmitChanges(); 更新匹配值后。

foreach (abcProduct p in abcProducts)
                {

                    var match = (from t in dc.abcProducts
                                 where t.sku == p.productcode
                                 select t).FirstOrDefault();

                    if (match == null)
                    {
                       //insertion code commented out
                        dc.abcProducts.InsertOnSubmit(prod);
                    }
                    else
                    {
                        ///.... setting up all fields. 
                        match.stockPublished = false;
                        match.imgPublished = false;
                        match.prodPublished = false;
                        match.lastUpdated = DateTime.Now;
                        dc.SubmitChanges(); //Call this otherwise you will
                                            //loose the match values in the next iteration
                    }
                }
于 2012-07-06T10:35:24.667 回答