locked
【Error】 An item with the same key has already been added RRS feed

  • Question

  • Now I 'm trying copy a entity by using code such as a following in CRM2013.

    EntityCollection entityCollection = _serviceProxy.RetrieveMultiple(query);
    Entity entity= entityCollection .Entities[0].ToEntity<Entity>();
    entity.Id = Guid.NewGuid();
    _serviceProxy.Create(entity); 

    But Error message Show "An item with the same key has already been added".

    If someone knows to solve the problem, please teach me.



    Saturday, February 28, 2015 12:51 PM

Answers

  • It is not a good way to create entities like that. You get this error because you have same attribute in the Entity context more than once. I suggest you to build the entity step by step.

    Also in my projects ı dont use the below code . I create the Entity object and set the attributes one by one. But if u want to use the short way , u can try the code below

    Entity e = collect.Entities[0].ToEntity<Entity>();
    
    Entity createentity = new Entity();
    createentity.LogicalName = e.LogicalName;
     foreach (var item in e.Attributes)
       {
           if (item.Key != e.LogicalName + "id")
                        createentity[item.Key] = item.Value;
       }
    service.Create(createentity);


    If you find this post helpful then please Vote as Helpful and Mark As Answer. Thanks and Regards, Polat Aydın My blog



    • Edited by Polat Aydın[MCP] Saturday, February 28, 2015 2:03 PM
    • Marked as answer by Indos Monday, March 2, 2015 10:57 PM
    Saturday, February 28, 2015 2:00 PM

All replies

  • It is not a good way to create entities like that. You get this error because you have same attribute in the Entity context more than once. I suggest you to build the entity step by step.

    Also in my projects ı dont use the below code . I create the Entity object and set the attributes one by one. But if u want to use the short way , u can try the code below

    Entity e = collect.Entities[0].ToEntity<Entity>();
    
    Entity createentity = new Entity();
    createentity.LogicalName = e.LogicalName;
     foreach (var item in e.Attributes)
       {
           if (item.Key != e.LogicalName + "id")
                        createentity[item.Key] = item.Value;
       }
    service.Create(createentity);


    If you find this post helpful then please Vote as Helpful and Mark As Answer. Thanks and Regards, Polat Aydın My blog



    • Edited by Polat Aydın[MCP] Saturday, February 28, 2015 2:03 PM
    • Marked as answer by Indos Monday, March 2, 2015 10:57 PM
    Saturday, February 28, 2015 2:00 PM
  • Thank you, Polat Aydın.

    I'll try that code you suggested at the beginning of the next week!!


    • Edited by Indos Saturday, February 28, 2015 2:08 PM
    Saturday, February 28, 2015 2:08 PM
  • Mr.Polat Aydın.

    I can solve my question. In my software, after create service, the key that was added already was added again. So, error was occurred.

    Your code was useful for solve another problem about GUID. 

    Thank you!

    Monday, March 2, 2015 10:56 PM