此EntitySet中已存在具有相同标识的实体

时间:2013-06-18 07:33:18

标签: .net wcf entity

关于该主题,我尝试了我在这里找到的所有链接

我有一个搜索记录的搜索操作

 public void SearchCampaignMembers()
    {

             EntityQuery<Lead> LeadQuery = from c in _service.SearchCampaignMembersQuery(Entity.FirstName, Entity.LastName)
                                          orderby c.LastName
                                          select c;
            OperationsListener listener = new OperationsListener(StringResources.Message_Processing);
            listener.AddOperationToListen(this._service.Load(LeadQuery, LoadBehavior.RefreshCurrent, loadOp =>
                    {
                        if (!loadOp.HasError)
                        {
                                CampaignSearchResultsViewModel vm = new CampaignSearchResultsViewModel();
                                foreach (Lead LeadObj in loadOp.Entities)
                                {
                                    vm.Leads.Add(LeadObj);
                                }

                                vm.diaBase = ViewController.OpenDialog("SearchCampaignResults", vm, r =>
                                 {
                                     LoadSingleEntity(vm.SelectedLead);
                                 });
                                                       }

                    }, true));
            listener.Start();
        }

    }

在listener.AddOperationToListen()之后,流程转到下面的代码

private void OnOperationCompleted(object sender, EventArgs e)
{
    OperationBase operation = (OperationBase)sender;
     if (operation.HasError)
     {
         if (operation is SubmitOperation)
         {
             SubmitOperation so = (SubmitOperation)operation;

             int count = 0;
             foreach (Entity entity in ((SubmitOperation)operation).EntitiesInError)
             {
                 if (entity.HasValidationErrors)
                 {
                     foreach (ValidationResult r in entity.ValidationErrors)
                     {
                         Errors.Add(r);
                         count++;
                     }
                 }
             }
             if (count == 0)
                 Errors.Add(so.Error);
         }
         else
             Errors.Add(operation.Error);

         if (IsReportingError)
             operation.MarkErrorAsHandled();
     }
     UpdateState();
}

然后程序通过一系列方法将搜索记录保存在数据库中......在一个中间方法中,我在 _service.LeadCampaignXrefs.Add(xref);

上得到了这个例外
    var old = Entity.LeadCampaigns.FirstOrDefault();
    if (old == null || old.CampaignId != SelectedCampaign.CampaignId)
    {

        var xref = new LeadCampaignXref()
        {
            CampaignId = SelectedCampaign.CampaignId
        };


        if (!_service.LeadCampaignXrefs.Contains(xref))
        {
        Entity.LeadCampaigns.Add(xref);
        }
        _service.LeadCampaignXrefs.Add(xref);

我曾尝试在SearchCampaignMembers()和不同位置分离/附加,结束,提交更改(),EntotySetClear()方法,但它给出了相同的结果。谁能让我知道如何/在哪里添加完全实现这些解决方案?

如果需要,我会提供更多代码

0 个答案:

没有答案