本文介绍了更新实体框架中的foreach循环中的记录6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在foreach循环中更新记录的问题。我有一个方法,从SQL表中导出文件,并将它们保存在一个净​​共享:

pre c $ public static void SyncOpportunity()
$ b使用(var ctx = new BPMOnline74Entities())
{
logger.Debug(从机会导入文件。
var oppFiles = from ctx.OpportunityFile
where SqlFunctions.DataLength(o.Data)> 0&& o.ServiceProcessed == false
选择o;

foreach(oppFiles中的OpportunityFile opp)
{
logger.Debug(string.Format({0} is being imported。,opp.Name));
string fileName =;
if(opp.OpportunityId == null)
{
fileName =E:\\BPM\\Opportunity\\;
logger.Error(文件没有绑定到任何数据库对象,保存在E:\\BPM\\Opportunity。);


{
try
{
fileName = PathInfo.GetPath(Opportunity,(Guid)opp.OpportunityId);

catch(Exception ex)
{
logger.Error(ex.Message.ToString());


try

File.WriteAllBytes(fileName + opp.Name,opp.Data);
logger.Debug(string.Format({0}从SQL导出,opp.Name));
opp.ServiceProcessed = true;
ctx.Entry(opp).State = System.Data.Entity.EntityState.Modified;
ctx.SaveChanges();

catch(Exception ex)
{
logger.Error(ex.Message.ToString());

$ b $ logger.Debug(string.Format(Imported {0} files。,oppFiles.Count()。ToString()));

logger.Debug(Disposing SyncOpportunity。);





从SQL成功导出文件后,我想更新记录的ServiceProcessed 字段为真,所以不会在我的服务的下一次迭代中被选中。问题是我的方法没有更新数据库,总是捕获我的所有记录。

解决方案

评论,因为实体框架上下文是(内部)使用模式,您需要只需调用 SaveChanges()一次,只有完成所有项的修改。 b
$ b

有关的详细信息 p>

I have a problem with updating records in a foreach loop. I have a method that exports files from SQL Table and saves them on a net share:

    public static void SyncOpportunity()
    {
        using (var ctx = new BPMOnline74Entities())
        {
            logger.Debug("Importing files from Opportunities.");
            var oppFiles = from o in ctx.OpportunityFile
                           where SqlFunctions.DataLength(o.Data) > 0 && o.ServiceProcessed == false
                           select o;

            foreach (OpportunityFile opp in oppFiles)
            {
                logger.Debug(string.Format("{0} is being imported.", opp.Name));
                string fileName="";
                if (opp.OpportunityId == null)
                {
                    fileName = "E:\\BPM\\Opportunity\\";
                    logger.Error("File not bound to any DB object. Saved in E:\\BPM\\Opportunity.");
                }
                else
                {
                    try
                    {
                        fileName = PathInfo.GetPath("Opportunity", (Guid)opp.OpportunityId);
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex.Message.ToString());
                    }
                }
                try
                {
                    File.WriteAllBytes(fileName + opp.Name, opp.Data);
                    logger.Debug(string.Format("{0} was exported from SQL.", opp.Name));
                    opp.ServiceProcessed = true;
                    ctx.Entry(opp).State = System.Data.Entity.EntityState.Modified;
                    ctx.SaveChanges();
                }
                catch(Exception ex)
                {
                    logger.Error(ex.Message.ToString());
                }
            }
            logger.Debug(string.Format("Imported {0} files.", oppFiles.Count().ToString()));
        }
        logger.Debug("Disposing SyncOpportunity.");
    }

After the file is successfully exported from SQL I want to update the record's "ServiceProcessed" field to true so it won't be selected on the next iteration of my service. The problem is that my method doesn't update the DB and always "catches" all of my records.

解决方案

As discussed in the comments, since Entity Framework context is (internally) employing the Unit of Work pattern, you need to call SaveChanges() only once and only after you've done modifying all the items.

More on MSDN

这篇关于更新实体框架中的foreach循环中的记录6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 22:26