0

我有两张表 Rule 和 RuleCondition (一个->很多)。

人们可以随时添加条件。

假设最初他添加了两个条件。他可以复出并添加另一个条件,也可以更新已经添加的条件。

我能够保存更新的条件,但无法插入他添加的额外条件。下面是我的代码,它失败了

rule.RuleConditions.Add(oRuleCon); -- Entity set was modified during enumaration

如果我使用这种方法

oAngieCtxt.RuleConditions.InsertOnSubmit(oRuleCon);

它根本没有插入数据。

有人可以建议如何处理吗?

public ActionResult saveMetricRule(Rule rule)
    {
        bool IsNew = rule.RuleId == 0;

        using (NewAngieDataContext oAngieCtxt = new NewAngieDataContext(new CSConfigurationMgr().GetConnectionString(ConnectionStringKey.Angie)))
        {
            if (IsNew)
                oAngieCtxt.Rules.InsertOnSubmit(rule);
            else
            {
                RuleCondition oRuleCon = null;
                foreach (RuleCondition childItem in rule.RuleConditions)
                {
                    if (childItem.RuleConditionId == 0)
                    {
                        oRuleCon = new RuleCondition();
                        oRuleCon.Points = childItem.Points;
                        oRuleCon.ConditionValue = childItem.ConditionValue;
                        oRuleCon.ToOperatorId = childItem.ToOperatorId;
                        oRuleCon.Sort = childItem.Sort;
                        rule.RuleConditions.Add(oRuleCon);
                  //   oAngieCtxt.RuleConditions.InsertOnSubmit(oRuleCon);
                    }
                    else
                    {
                        oRuleCon =
                            oAngieCtxt.RuleConditions
                            .Where(CON => CON.RuleConditionId == childItem.RuleConditionId)
                            .FirstOrDefault();

                        oRuleCon.Points = childItem.Points;
                        oRuleCon.ConditionValue = childItem.ConditionValue;
                        oRuleCon.ToOperatorId = childItem.ToOperatorId;
                        oRuleCon.Sort = childItem.Sort;
                    }
                }

                oAngieCtxt.Rules.Attach(rule);
                oAngieCtxt.Refresh(RefreshMode.KeepCurrentValues, rule);
            }
            oAngieCtxt.SubmitChanges();
        }

        return this.Json(new
        {
            msg = "Successful save.",
            ruleId = rule.RuleId
        });
    }
4

1 回答 1

0

您不能将项目添加到您正在枚举的列表中。由于您正在循环,因此rule.RuleConditions您不能在foreach循环内添加它。相反,您可以添加到临时列表,然后将该列表中的所有项目添加rule.RuleConditionsforeach.

var newRuleConditions = new List<RuleCondition>();
foreach (RuleCondition childItem in rule.RuleConditions)
{
  if (childItem.RuleConditionId == 0)
  {
    oRuleCon = new RuleCondition();
    oRuleCon.Points = childItem.Points;
    oRuleCon.ConditionValue = childItem.ConditionValue;
    oRuleCon.ToOperatorId = childItem.ToOperatorId;
    oRuleCon.Sort = childItem.Sort;
    //add to temporary list
    newRuleConditions.Add(oRuleCon);
    oAngieCtxt.RuleConditions.InsertOnSubmit(oRuleCon);
  }
  else
  {
    ...
  }
}
//add all new rule conditions
rule.RuleConditions.AddRange(newRuleConditions);
于 2013-09-20T00:09:49.330 回答