Try our conversational search powered by Generative AI!

Add Assets to Variants


Trying to add images to variants.

The code looks like this:

var parentLink = ReferenceConverter.Service.GetContentLink(parentCode);

F parent = ContentRepository.Service.TryGet(parentLink.ToReferenceWithoutVersion(), _catalogService.Culture, out parent) ? parent.CreateWritableClone<F>() : ContentRepository.Service.GetDefault<F>(parentLink, _catalogService.Culture);

ContentRepository.Service.Save(parent, SaveAction.Publish, AccessLevel.NoAccess);

This works well the first time, but if the import should handle a change for this article again at a later date, the code will crash.

error message:
"Violation of PRIMARY KEY constraint 'PK_ecfVersionAsset'. Cannot insert duplicate key in object 'dbo.ecfVersionAsset'. The duplicate key value is (8078274, image.jpg, a910104b-a4ee-44e8-8cd3-33950f19d9ae)." has been terminated. "

Any suggestions on what's wrong, how to get on?

Jan 24, 2020 13:09

CommerceMediaCollection.Add is only an add, it doesn't do update.

You have to remove the old commerce media with the same filename from the collection before adding the update one.

Of course that would only detach the image from the variant, most likely you would want to remove the old image also.

Jan 24, 2020 15:41
Ompa - Feb 24, 2020 15:46
Of course that is so.

Thanks for the help!

As Erik says, you should only add an item once. Your code can be changed so it check for existing commerceMedia first before adding 

Jan 27, 2020 8:51
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.