将孩子和父linq一起保存到sql

时间:2013-09-19 12:00:35

标签: c# asp.net-mvc linq-to-sql

我有两个表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
        });
    }

1 个答案:

答案 0 :(得分:0)

您无法将项目添加到您要枚举的列表中。由于您循环遍历rule.RuleConditions,因此无法在foreach循环内添加它。相反,您可以添加到临时列表,然后将该列表中的所有项目添加到rule.RuleConditions之后的foreach

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);