Orchard CMS Quick Tip–A duplicate value cannot be inserted into a unique index

Work on an Orchard CMS widget had been going pretty well. It display OK and didn’t seem like it had any problems but each time I tried to save anything I’d get an exception thrown.

The exception started off like this:

A duplicate value cannot be inserted into a unique index. [ Table name = Common_IdentityPartRecord...

I had added an IdentityPart to the widget because I thought I had read somewhere that adding the IdentityPart was a best-practice for supporting the import / export module.

It turned out it was not.

After a quick search I found a post by Piotr Szmyd which explained the reasoning behind this:

You can't use Unique constraint when dealing with content part records. The reason is that those are created early, when no data has been passed to it yet (with all properties being empty/null). This happens inside ContentManager. Hence, all properties on those kinds of records need to be nullable and non-unique.

But you can use the unique/not-null constraints on classes that are mapped to records you create and use via IRepository directly.

If you have added the IdentityPart to a content part by accident to a content part then you can remove it with a data migration.

  1. Open up your Migrations class (often called “Migrations.cs” or in the “.\Migrations\” folder).
  2. Assuming that your content part is called “” the code would look like this:

    public int UpdateFrom1() {
            "FeaturedProductWidget", cfg => cfg
        return 2;

Obviously you should also update the UpdateFrom1() to whatever update version your migrations class is currently on.

No comments :