0

我有一个与新 InputOutputMap 相关的新 ParameterPart 实体。InputOutputMap 有几个我从数据库中提取的 InputStates,我需要与 InputOutputMap 关联。如何使用 MVC 和 WCF 数据服务将此链保存到数据库?我正在使用以下代码(通过 ajax 调用调用,上下文仅在构造函数的第一次调用中设置)但遇到了几个问题:

  • 当我使用 AddLink(如下所示)时,我可以在第一次尝试时添加数据(无论我必须关联多少输入状态)。但是,如果再次调用该方法(通过 ajax),我只能在指定一个输入状态时添加数据。如果我有多个输入状态,我会得到上下文已经在跟踪关系。请注意,该方法是使用 ajax 调用来调用的。
  • 我曾尝试使用 SetLink、AddRelatedObject 和 attach,但每次在上述情况下都会出错。有时,错误是上下文已经在跟踪实体或关系。在其他时候,上下文不跟踪实体。
  • 当我在方法而不是构造函数中设置上下文时,我没有得到任何好处。

    if (vm != null)
        {
            ParameterPart parameterPart = null;
    
            // Create a new parameter
            if (vm.PartNumberId == 0)
            {
                // Create an instance of ParameterPart
                parameterPart = new ParameterPart()
                {
                    Description = vm.ParameterDescription                        
                };
    
                // Save the ParameterPart into the database
                try
                {
                    ctx.AddToParameterParts(parameterPart);
                    ctx.SaveChanges();
                }
                catch (System.Exception ex)
                {
                    throw;
                }
            }
            else
            {
                // Fetch the existing parameter
                parameterPart = new ParameterPart();
                parameterPart = (from pp in ctx.ParameterParts
                                 where pp.PartNumberId == vm.PartNumberId
                                 select pp).Single();
    
                // Update the ParameterPart from the vm
                parameterPart.Description = vm.ParameterDescription;
            }
    
            if (parameterPart != null)
            {
                if (vm.StateValues.Count > 0)
                {
                    InputOutputMap inputOutputMap = new InputOutputMap();
                    inputOutputMap.PartNumberId = parameterPart.PartNumberId;
    
                    ctx.AddToInputOutputMaps(inputOutputMap);
    
                    // Prepare a new InputOutputMap
                    foreach (var state in vm.StateValues)
                    {
                        if (state.InputStateId != 0)
                        {
                            // Fetch the inputstate
                            var inputState = (from i in ctx.InputStates
                                              where i.InputStateId == state.InputStateId
                                              select i).Single();
    
                            try
                            {
                                ctx.AddLink(inputOutputMap, "InputStates", inputState);
                                ctx.SaveChanges();
                            }
                            catch (System.Exception ex)
                            {
                                throw;
                            }
                        }                            
                    }
                }
            }
        }
    
4

1 回答 1

0

AddLink 方法按预期工作。由于数据问题,我收到错误消息。

于 2012-06-26T20:09:19.387 回答