Dynamics 365 (CRM) Version 9.1 online C# code Error: Entity Id must be the same as the value set in property bag

c# dynamics-crm dynamics-crm-online entity-framework entity-framework-6

Question

When cloning/copying child records, I use a foreach loop and then create a record with all its attributes. I wrote similar code in another project and worked fine for me.

There are multiple articles/questions based on the same Error. Now my issue is how should I create child records with all its attributes.

foreach (var packingList in oPEntityCollection.Entities)
{                                        
    packingList.Attributes.Remove("statuscode");
    packingList.Attributes.Remove("statecode"); 
    packingList.Id=Guid.Empty; 
    orgService.Create(packingList);
}

Another strange issue

An entry with the same key already exists

Code:

Entity parentEntity = orgService.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet(true));    
parentEntity.Id = Guid.empty;
orgService.Create(parentEntity);

Even if I create a new object and copy parentEntity just like below I get this error.

Entity costcalEntity = new Entity();

costcalEntity = parentEntity;
costcalEntity.Id = Guid.Empty;
orgService.Create(costcalEntity);

So I end up creating a record with primary name and once the record is created, I update the same record with old record attributes.

Entity costcalEntity = new Entity();
costcalEntity.LogicalName = parentEntity.LogicalName;
costcalEntity["name"] = parentQuotationEntity.GetAttributeValue<string>("name");
costcalEntity.Id = Guid.Empty;
Guid newGuid = orgService.Create(costcalEntity);
if (newGuid != Guid.Empty)
{
    costcalEntity = parentEntity;
    costcalEntity.Id = newGuid;
    orgService.Update(costcalEntity);
}

and this works fine.

1
0
3/24/2020 7:22:30 PM

Popular Answer

In both cases you have the same issue, with it's root cause being the Id stored in the attribute collection of the entity. If you look at the early bound generation, you can access the Id by the entity.Id property, as well as the attribute collection as shown in the definition for the id in the primary id:

public System.Nullable<System.Guid> AccountId
{
    [System.Diagnostics.DebuggerNonUserCode()]
    get
    {
        return this.GetAttributeValue<System.Nullable<System.Guid>>("accountid");
    }
    [System.Diagnostics.DebuggerNonUserCode()]
    set
    {
        this.OnPropertyChanging("AccountId");
        this.SetAttributeValue("accountid", value);
        if (value.HasValue)
        {
            base.Id = value.Value;
        }
        else
        {
            base.Id = System.Guid.Empty;
        }
        this.OnPropertyChanged("AccountId");
    }
}

So when you are retrieving an existing entity, both the Property Id, which you have handled, as well as the attribute collection, which you haven't handled, have been populated by the CRM SDK. So in order to be able to duplicate it, you'll need to clear the id in both places.

1
3/24/2020 11:25:25 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow