Custom plugin with multiple tables Nop 4.1

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
5 years ago
Take a look at Tax.FixedOrByCountryStateZip Plugin > Data > CountryStateZipObjectContext.cs
5 years ago
I have the DependencyRegistrar figured out but I some how need to further configure the Model in the ObjectContext class. how do I configure 2 instead of 1. code below shows 1.... I did not find any plugins available that have 2 db tables.


/// <summary>
        /// Further configuration the model
        /// </summary>
        /// <param name="modelBuilder">Model muilder</param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.ApplyConfiguration(new TaxRateMap());
            base.OnModelCreating(modelBuilder);
        }
5 years ago
mcselasvegas wrote:
I have the DependencyRegistrar figured out but I some how need to further configure the Model in the ObjectContext class. how do I configure 2 instead of 1. code below shows 1.... I did not find any plugins available that have 2 db tables.


/// <summary>
        /// Further configuration the model
        /// </summary>
        /// <param name="modelBuilder">Model muilder</param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.ApplyConfiguration(new TaxRateMap());
            base.OnModelCreating(modelBuilder);
        }


DbContextOptions only accepts one so you will need to create two.
5 years ago
Hello,

=> following steps are for create mulitple tables.

Step:1 create Entity class with using BaseEntity interface

public class Table1Record : BaseEntity
{
    public int Property1 { get; set; }
    public string Property2 { get; set; }
}

public class Table2Record : BaseEntity
{
    public int Property1 { get; set; }
    public string Property2 { get; set; }
}

step:2 create calss for used the entity

public partial class Table1RecordMap : NopEntityTypeConfiguration<Table1Record>
{
    public override void Configure(EntityTypeBuilder<Table1Record> builder)
    {
      builder.ToTable(nameof(Table1Record));
      builder.HasKey(table1Map => table1Map.Id);

      base.Configure(builder);
    }
}

public partial class Table2RecordMap : NopEntityTypeConfiguration<Table1Record>
{
    public override void Configure(EntityTypeBuilder<Table2Record> builder)
    {
      builder.ToTable(nameof(Table2Record));
      builder.HasKey(table2Map => table2Map.Id);

      base.Configure(builder);
    }
}

step:3 create one objectContext class file and using interface IDbcontext,and DbContext for ovveride OnModelCreating method.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
      modelBuilder.ApplyConfiguration(new Table1RecordMap());
      modelBuilder.ApplyConfiguration(new Table2RecordMap());
      modelBuilder.ApplyConfiguration(new Table3RecordMap());
            base.OnModelCreating(modelBuilder);
}

step:4 registrar in DependencyRegistrar , create class and use IDependencyRegistrar interface

public void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config)
{
      //Add your plugin ObjectContext file
      builder.RegisterPluginDataContext<ObjectContext>("nop_object_context_TableRecord");

            //override required repository with our custom context
            builder.RegisterType<EfRepository<Table1Record>>().As<IRepository<Table1Record>>()
                .WithParameter(ResolvedParameter.ForNamed<IDbContext>("nop_object_context_TableRecord"))
                .InstancePerLifetimeScope();

            builder.RegisterType<EfRepository<Table2Record>>().As<IRepository<Table2Record>>()
                .WithParameter(ResolvedParameter.ForNamed<IDbContext>("nop_object_context_TableRecord"))
                .InstancePerLifetimeScope();
}

step:5 create one class file and use INopstartup interface

public class NopStartup : INopStartup
{
        /// <summary>
        /// Add and configure any of the middleware
        /// </summary>
        /// <param name="services">Collection of service descriptors</param>
        /// <param name="configuration">Configuration of the application</param>
        public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            //add object context
            services.AddDbContext<PluginObjectContext>(optionsBuilder =>
            {
                optionsBuilder.UseSqlServerWithLazyLoading(services);
            });

            services.Configure<RazorViewEngineOptions>(options =>
            {
                options.ViewLocationExpanders.Add(new ViewLocationExpander());
            });
        }

        /// <summary>
        /// Configure the using of added middleware
        /// </summary>
        /// <param name="application">Builder for configuring an application's request pipeline</param>
        public void Configure(IApplicationBuilder application)
        {
        }

        /// <summary>
        /// Gets order of this startup configuration implementation
        /// </summary>
        public int Order => 1001;
}

and plugin Install method time call  objectContext class file install method that time your multiple tables will be created.
it is  help for you.
Thank you
Sagar Kayasth
5 years ago
Shegy wrote:
Hello,

=> following steps are for create mulitple tables.

Step:1 create Entity class with using BaseEntity interface

public class Table1Record : BaseEntity
{
    public int Property1 { get; set; }
    public string Property2 { get; set; }
}

public class Table2Record : BaseEntity
{
    public int Property1 { get; set; }
    public string Property2 { get; set; }
}

step:2 create calss for used the entity

public partial class Table1RecordMap : NopEntityTypeConfiguration<Table1Record>
{
    public override void Configure(EntityTypeBuilder<Table1Record> builder)
    {
      builder.ToTable(nameof(Table1Record));
      builder.HasKey(table1Map => table1Map.Id);

      base.Configure(builder);
    }
}

public partial class Table2RecordMap : NopEntityTypeConfiguration<Table1Record>
{
    public override void Configure(EntityTypeBuilder<Table2Record> builder)
    {
      builder.ToTable(nameof(Table2Record));
      builder.HasKey(table2Map => table2Map.Id);

      base.Configure(builder);
    }
}

step:3 create one objectContext class file and using interface IDbcontext,and DbContext for ovveride OnModelCreating method.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
      modelBuilder.ApplyConfiguration(new Table1RecordMap());
      modelBuilder.ApplyConfiguration(new Table2RecordMap());
      modelBuilder.ApplyConfiguration(new Table3RecordMap());
            base.OnModelCreating(modelBuilder);
}

step:4 registrar in DependencyRegistrar , create class and use IDependencyRegistrar interface

public void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config)
{
      //Add your plugin ObjectContext file
      builder.RegisterPluginDataContext<ObjectContext>("nop_object_context_TableRecord");

            //override required repository with our custom context
            builder.RegisterType<EfRepository<Table1Record>>().As<IRepository<Table1Record>>()
                .WithParameter(ResolvedParameter.ForNamed<IDbContext>("nop_object_context_TableRecord"))
                .InstancePerLifetimeScope();

            builder.RegisterType<EfRepository<Table2Record>>().As<IRepository<Table2Record>>()
                .WithParameter(ResolvedParameter.ForNamed<IDbContext>("nop_object_context_TableRecord"))
                .InstancePerLifetimeScope();
}

step:5 create one class file and use INopstartup interface

public class NopStartup : INopStartup
{
        /// <summary>
        /// Add and configure any of the middleware
        /// </summary>
        /// <param name="services">Collection of service descriptors</param>
        /// <param name="configuration">Configuration of the application</param>
        public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            //add object context
            services.AddDbContext<PluginObjectContext>(optionsBuilder =>
            {
                optionsBuilder.UseSqlServerWithLazyLoading(services);
            });

            services.Configure<RazorViewEngineOptions>(options =>
            {
                options.ViewLocationExpanders.Add(new ViewLocationExpander());
            });
        }

        /// <summary>
        /// Configure the using of added middleware
        /// </summary>
        /// <param name="application">Builder for configuring an application's request pipeline</param>
        public void Configure(IApplicationBuilder application)
        {
        }

        /// <summary>
        /// Gets order of this startup configuration implementation
        /// </summary>
        public int Order => 1001;
}

and plugin Install method time call  objectContext class file install method that time your multiple tables will be created.
it is  help for you.
Thank you
Sagar Kayasth


Thank you so much... Clear and Precise! your post helped me identify my problem. I would vote you up 10 Times if I could!!!
4 years ago
Shegy wrote:
Hello,

=> following steps are for create mulitple tables.

Step:1 create Entity class with using BaseEntity interface

public class Table1Record : BaseEntity
{
    public int Property1 { get; set; }
    public string Property2 { get; set; }
}

public class Table2Record : BaseEntity
{
    public int Property1 { get; set; }
    public string Property2 { get; set; }
}

step:2 create calss for used the entity

public partial class Table1RecordMap : NopEntityTypeConfiguration<Table1Record>
{
    public override void Configure(EntityTypeBuilder<Table1Record> builder)
    {
      builder.ToTable(nameof(Table1Record));
      builder.HasKey(table1Map => table1Map.Id);

      base.Configure(builder);
    }
}

public partial class Table2RecordMap : NopEntityTypeConfiguration<Table1Record>
{
    public override void Configure(EntityTypeBuilder<Table2Record> builder)
    {
      builder.ToTable(nameof(Table2Record));
      builder.HasKey(table2Map => table2Map.Id);

      base.Configure(builder);
    }
}

step:3 create one objectContext class file and using interface IDbcontext,and DbContext for ovveride OnModelCreating method.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
      modelBuilder.ApplyConfiguration(new Table1RecordMap());
      modelBuilder.ApplyConfiguration(new Table2RecordMap());
      modelBuilder.ApplyConfiguration(new Table3RecordMap());
            base.OnModelCreating(modelBuilder);
}

step:4 registrar in DependencyRegistrar , create class and use IDependencyRegistrar interface

public void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config)
{
      //Add your plugin ObjectContext file
      builder.RegisterPluginDataContext<ObjectContext>("nop_object_context_TableRecord");

            //override required repository with our custom context
            builder.RegisterType<EfRepository<Table1Record>>().As<IRepository<Table1Record>>()
                .WithParameter(ResolvedParameter.ForNamed<IDbContext>("nop_object_context_TableRecord"))
                .InstancePerLifetimeScope();

            builder.RegisterType<EfRepository<Table2Record>>().As<IRepository<Table2Record>>()
                .WithParameter(ResolvedParameter.ForNamed<IDbContext>("nop_object_context_TableRecord"))
                .InstancePerLifetimeScope();
}

step:5 create one class file and use INopstartup interface

public class NopStartup : INopStartup
{
        /// <summary>
        /// Add and configure any of the middleware
        /// </summary>
        /// <param name="services">Collection of service descriptors</param>
        /// <param name="configuration">Configuration of the application</param>
        public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            //add object context
            services.AddDbContext<PluginObjectContext>(optionsBuilder =>
            {
                optionsBuilder.UseSqlServerWithLazyLoading(services);
            });

            services.Configure<RazorViewEngineOptions>(options =>
            {
                options.ViewLocationExpanders.Add(new ViewLocationExpander());
            });
        }

        /// <summary>
        /// Configure the using of added middleware
        /// </summary>
        /// <param name="application">Builder for configuring an application's request pipeline</param>
        public void Configure(IApplicationBuilder application)
        {
        }

        /// <summary>
        /// Gets order of this startup configuration implementation
        /// </summary>
        public int Order => 1001;
}

and plugin Install method time call  objectContext class file install method that time your multiple tables will be created.
it is  help for you.
Thank you
Sagar Kayasth


Awsome Work Sagar.
I have one another requirement in that. I want to make one to many relationships between Table1Record and Table2Record(Table2Record will have multiple Table1Record records).
How can I achive this.

Thank you in Advance.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.