Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在foreach循环c中使用savechanges()#_C#_Asp.net_Entity Framework_Asp.net Mvc 3 - Fatal编程技术网

C# 在foreach循环c中使用savechanges()#

C# 在foreach循环c中使用savechanges()#,c#,asp.net,entity-framework,asp.net-mvc-3,C#,Asp.net,Entity Framework,Asp.net Mvc 3,我下面有一个方法,它将数据写入数据库中的两个表。我需要在foreach部分将一个集合写入数据库。为什么saveChanges在循环的每次迭代中都不起作用,有没有更好的方法? 有两件事。。。第一:函数不返回任何东西。。。。第二:数据库不是最新的。。。。当我从函数中删除savechange()时,它可以工作,但不会更改数据库中的值 if (_context.ProductPins.Where(a => a.ProductId == id && a.Status==false).

我下面有一个方法,它将数据写入数据库中的两个表。我需要在foreach部分将一个集合写入数据库。为什么saveChanges在循环的每次迭代中都不起作用,有没有更好的方法? 有两件事。。。第一:函数不返回任何东西。。。。第二:数据库不是最新的。。。。当我从函数中删除savechange()时,它可以工作,但不会更改数据库中的值

if (_context.ProductPins.Where(a => a.ProductId == id && a.Status==false).Count() > 0)
        {
            var c = 0;
            var ProductPins = _context.ProductPins.Where(a=>a.Status==false && a.ProductId == id);
            foreach(var item in ProductPins)
            {
                ApplicationUser.Balance = ApplicationUser.Balance - product.BuyingPrice;
                item.Equals(true);
                _context.Statements.Add(new Statement
                {
                    Amount = product.BuyingPrice,
                    RecordDate = DateTime.Now,
                    Destination = false,
                    FromApplicationUserId = _userManager.GetUserId(User),
                    //ToApplicationUserId = nu,
                    BalanceType = BalanceType.currentbalance,
                });

                _context.Update(ApplicationUser);
                _context.Update(item);
                _context.SaveChanges();
                c++;
                if (c > count)
                {
                    break;
                }





            }
我只将savechange()方法放在了froreach循环之外……savechanges()不应该在循环内部

if (_context.ProductPins.Where(a => a.ProductId == id && a.Status==false).Count() > 0)
        {
            var c = 0;
            var ProductPins = _context.ProductPins.Where(a=>a.Status==false && a.ProductId == id);
            foreach(var item in ProductPins)
            {
                ApplicationUser.Balance = ApplicationUser.Balance - product.BuyingPrice;
                item.Equals(true);
                _context.Statements.Add(new Statement
                {
                    Amount = product.BuyingPrice,
                    RecordDate = DateTime.Now,
                    Destination = false,
                    FromApplicationUserId = _userManager.GetUserId(User),
                    //ToApplicationUserId = nu,
                    BalanceType = BalanceType.currentbalance,
                });

                _context.Update(ApplicationUser);
                _context.Update(item);

                c++;
                if (c > count)
                {
                    break;
                }





            }
_context.SaveChanges();

如果要保存到数据库,则应将这些更改排入队列,以便可以在最后执行一次写入并一次处理所有更改,而不是一次处理一次,这样可以利用数据库内部的写入处理。

是否使用实体框架?我知道Dapper允许您实际执行基于集合的插入,但看起来实体框架是正确的吗?请指定“不起作用”的含义。是否有错误?例外?没有错误,但数据未保存?是否保存了数据但未正确保存?或者它是否符合规范,但您只是不喜欢代码结构?@Greg是的,我使用的是实体framework@JohnWu有两件事。。。第一:函数不返回任何东西。。。。第二:数据库不是最新的。。。。当我从函数中删除savechange()时,它可以工作,但不会更改数据库中的值@محمدلعاني-如果您将sql server作为后端运行,是否可以使用sql profiler进行检查?SaveChanges()应返回int,显示保存的记录数。(另一方面,您可以将_context.ProductPins.Where(a=>a.ProductId==id&&a.Status==false).Count()>0更改为_context.ProductPins.Any(a=>a.ProductId==id&&a.Status==false)并将_userManager.GetUserId(User)置于循环之外)。