Creating Multiple Tables in Database On Plugin Installation

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
Il y a 3 ans
Hello everyone, I'm new to NopCommerce and I'm trying to develop a custom plugin and I'm facing some issue. I'm trying to create multiple tables on plugin installation. I'm successful in creating one table on installation but when I'm trying to create two or more tables it's skipping them after creating one table. When I'm uninstalling the plugin it tries to drop the other tables as well even though they weren't created, which cause my program to crash.

Here is my code for creating one table and it's working fine:


using FluentMigrator;
using Nop.Data.Migrations;
using Nop.Plugin.Misc.BoxPacking.Domains;

namespace Nop.Plugin.Misc.BoxPacking.Data
{
    [SkipMigrationOnUpdate]
    [NopMigration("2020/05/27 08:40:55:1687541", "Other.Boxes base schema")]
    public class SchemaMigration : AutoReversingMigration
    {
        protected IMigrationManager _migrationManager;

        public SchemaMigration(IMigrationManager migrationManager)
        {
            _migrationManager = migrationManager;
        }

        public override void Up()
        {
            _migrationManager.BuildTable<OrderBoxes>(Create);
        }
    }
}



using Microsoft.Extensions.DependencyInjection;
using Nop.Core.Configuration;
using Nop.Core.Infrastructure;
using Nop.Core.Infrastructure.DependencyManagement;
using Nop.Plugin.Misc.BoxPacking.Services;

namespace Nop.Plugin.Misc.BoxPacking.Infrastructure
{
    /// <summary>
    /// Dependency registrar
    /// </summary>
    public class DependencyRegistrar : IDependencyRegistrar
    {
        /// <summary>
        /// Register services and interfaces
        /// </summary>
        /// <param name="services">Collection of service descriptors</param>
        /// <param name="typeFinder">Type finder</param>
        /// <param name="appSettings">App settings</param>
        public virtual void Register(IServiceCollection services, ITypeFinder typeFinder, AppSettings appSettings)
        {
            services.AddScoped<IOrderBoxesServices, OrderBoxesServices>();
        }

        /// <summary>
        /// Order of this dependency registrar implementation
        /// </summary>
        public int Order => 1;
    }
}


Here is my code for creating multiple tables which is not working:


using FluentMigrator;
using Nop.Data.Migrations;
using Nop.Plugin.Misc.BoxPacking.Domains;

namespace Nop.Plugin.Misc.BoxPacking.Data
{
    [SkipMigrationOnUpdate]
    [NopMigration("2020/05/27 08:40:55:1687541", "Other.Boxes base schema")]
    public class SchemaMigration : AutoReversingMigration
    {
        protected IMigrationManager _migrationManager;

        public SchemaMigration(IMigrationManager migrationManager)
        {
            _migrationManager = migrationManager;
        }

        public override void Up()
        {
            _migrationManager.BuildTable<OrderBoxes>(Create);
            _migrationManager.BuildTable<Boxes>(Create);
        }
    }
}



using Microsoft.Extensions.DependencyInjection;
using Nop.Core.Configuration;
using Nop.Core.Infrastructure;
using Nop.Core.Infrastructure.DependencyManagement;
using Nop.Plugin.Misc.BoxPacking.Services;

namespace Nop.Plugin.Misc.BoxPacking.Infrastructure
{
    /// <summary>
    /// Dependency registrar
    /// </summary>
    public class DependencyRegistrar : IDependencyRegistrar
    {
        /// <summary>
        /// Register services and interfaces
        /// </summary>
        /// <param name="services">Collection of service descriptors</param>
        /// <param name="typeFinder">Type finder</param>
        /// <param name="appSettings">App settings</param>
        public virtual void Register(IServiceCollection services, ITypeFinder typeFinder, AppSettings appSettings)
        {
            services.AddScoped<IOrderBoxesServices, OrderBoxesServices>();
            services.AddScoped<IBoxesServices, BoxesServices>();
        }

        /// <summary>
        /// Order of this dependency registrar implementation
        /// </summary>
        public int Order => 1;
    }
}
Il y a 3 ans
If your  OrderBoxes table already exists when the Up Method is run then that will create an error

public override void Up()
        {
            _migrationManager.BuildTable<OrderBoxes>(Create);
            _migrationManager.BuildTable<Boxes>(Create);
        }


Change you up Method

public override void Up()
{
    try
    {
        // Try to create database tables
        if (!Schema.Schema("dbo").Table(NameCompatibilityManager.GetTableName(typeof(OrderBoxes))).Exists())
            _migrationManager.BuildTable<OrderBoxes>(Create);

        if (!Schema.Schema("dbo").Table(NameCompatibilityManager.GetTableName(typeof(Boxes))).Exists())
            _migrationManager.BuildTable<Boxes>(Create);
    }
    catch
    {
        // The Database Files may still exist if not deleted from last unistall
    }
}
Il y a 3 ans
It's not working it's still creating one table on installation. I want my plugin to create two or more tables on installation and drop those tables when uninstalled.
Il y a 3 ans
Naonll wrote:
It's not working it's still creating one table on installation. I want my plugin to create two or more tables on installation and drop those tables when uninstalled.
do you have checked error log from admin -> system -> log -> view ( if found any relared error)
Il y a 3 ans
On installation there is no error and when I uninstall the plugin the application throw an exception where it says like "There is no Boxes table in database".
Il y a 3 ans
Naonll wrote:
On installation there is no error and when I uninstall the plugin the application throw an exception where it says like "There is no Boxes table in database".


can you please try with changing migration date ->' [NopMigration("2020/05/27 08:40:55:1687541", "Other.Boxes base schema")]'
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.