Unable to resolve service for type OrchardCore.ContentManagement.Metadata.IContentDefinitionManager

868 views Asked by At

I have created a class 'ProductPart' where these entities will be created in Sql DB.

 public class ProductPart : ContentPart
    {
        public decimal UnitPrice { get; set; }
        public string Sku { get; set; }
    }

To represent those entities in SQL we have to go through Migrations and the class look like the one below

 public class Migrations : DataMigration
    {
        private readonly IContentDefinitionManager _contentDefinitionManager;
        public Migrations(IContentDefinitionManager contentDefinitionManager)
        {
            this._contentDefinitionManager = contentDefinitionManager;
        }
        public int Create()
        {
            CreateTable();
            UpdateForm1();
            return 1;
        }

        private void CreateTable()
        {
            SchemaBuilder.CreateTable(nameof(ProductPart), table => table
           .Column<int>("Id", column => column.PrimaryKey().NotNull())
           .Column<decimal>("UnitPrice")
           .Column<string>("Sku", column => column.WithLength(50))
            );
        }

        private void UpdateForm1()
        {
            _contentDefinitionManager.AlterPartDefinition(nameof(ProductPart), part => 
                 part.Attachable()
                .WithDisplayName("Product")
                .WithDescription("Add products to the content."));
        }

        
    }

And in the startup class, I have created a scope instance, which is

 public void ConfigureServices(IServiceCollection services)
        {   
            services.AddContentPart<ProductPart>();
            services.AddScoped<IDataMigration, Migrations>();
            services.AddOrchardCms();
            
        }

Finally, when I ran this I am getting the below error.

*> Some services are not able to be constructed (Error while validating

the service descriptor 'ServiceType: OrchardCore.Data.Migration.IDataMigration Lifetime: Scoped ImplementationType: OrchardWebSiteModule.Migrations': Unable to resolve service for type 'OrchardCore.ContentManagement.Metadata.IContentDefinitionManager' while attempting to activate 'OrchardWebSiteModule.Migrations'.)*

1

There are 1 answers

0
Eric On

I got exactly the same problem with you and after a while finding out, I found a solution that may help you. First you can reference this issue on Github Link . So following the explanation on that issue, you need to enable Content module in your Manifest.cs file in you custom module, my Manifest.cs file below:

Manifest.cs

After that, you need one more step is to move this line services.AddScoped<IDataMigration, Migrations>(); from Startup.cs in the app to the one in your custom module. I'm not sure what the exact reason is but I guess that the content module is not enabled in the main app.