orchard cms content part, field gets added twice and can't remove

136 Views Asked by At

I added a simple field to a content part

ContentDefinitionManager.AlterPartDefinition("CarouselContentPart", b =>
            b
            .WithField("AutoPlay", cfg =>
                cfg.OfType("NumericField")
                    .WithSetting("NumericFieldSettings.Hint", "Enter a value in milliseconds for autoplay (leave blank for no autoplay)")
                    .WithSetting("NumericFieldSettings.Required", "false")
                    .WithDisplayName("Autoplay"))
            );

        return 18;

its got added twice and now I can't remove it, either through another migration or through the back office. this is the stack trace

An unhandled exception has occurred and the request was terminated. Please refresh the page. If the error persists, go back Sequence contains more than one matching element System.InvalidOperationException: Sequence contains more than one matching element at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable1 source, Func2 predicate) at Orchard.ContentManagement.MetaData.Builders.ContentPartDefinitionBuilder.RemoveField(String fieldName) in C:\Development\FE-Corporate\src\Orchard\ContentManagement\MetaData\Builders\ContentPartDefinitionBuilder.cs:line 46 at Orchard.ContentTypes.Services.ContentDefinitionService.<>c__DisplayClass48.b__47(ContentPartDefinitionBuilder typeBuilder) at Orchard.ContentManagement.MetaData.ContentDefinitionManagerExtensions.AlterPartDefinition(IContentDefinitionManager manager, String name, Action1 alteration) in C:\Development\FE-Corporate\src\Orchard\ContentManagement\MetaData\IContentDefinitionManager.cs:line 32 at Orchard.ContentTypes.Services.ContentDefinitionService.RemoveFieldFromPart(String fieldName, String partName) at Orchard.ContentTypes.Controllers.AdminController.RemoveFieldFromPOST(String id) at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3d() at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.b__3f() at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.b__3f() at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.b__3f() at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.b__3f()

1

There are 1 best solutions below

0
On

If you don't have any important data in your new content part yet you can try just dropping and recreating it with a migration.

This will make you lose all the data saved for the content part but will also give you a chance to make a clean break.

You can remove the content part like this:

    public int UpdateFrom18() {
        // remove the CarouselContentPart part cleanly
        ContentDefinitionManager.DeletePartDefinition(typeof (CarouselContentPart).Name);
enter code here
        // re-add the CarouselContentPart again
        // NOTE: Change this bit to add include whatever you have created in your previous 17 migrations such as setting up the part and adding fields
        ContentDefinitionManager.AlterPartDefinition(typeof (CarouselContentPart).Name,
            cfg = > cfg
            .Attachable()
            .WithDescription("Some kind of description, etc"));
        return 4;
    }