如何使用WCF数据服务保存具有相关实体集合的实体

时间:2012-04-11 19:05:32

标签: asp.net-mvc wcf-data-services odata savechanges

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

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

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

1 个答案:

答案 0 :(得分:0)

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