Custom plugin with multiple tables Nop 4.1

1 2 >
Posted: August 22, 2018 at 6:26 PM Quote #210926
I am trying to write a custom plugin that creates multiple tables on install. Everything works fine with one table and the code below. but if I try to do two tables I get an Ambiguous error message

Search the internet but can not find the solution. Any help would be appreciated.

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

            //modelBuilder.ApplyConfiguration(new Table2RecordMap());
            //base.OnModelCreating(modelBuilder);
        }
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: August 22, 2018 at 9:00 PM Quote #210929
Hi,

Can you share full error message?

Also make sure table names are unique and EntityMap class file.
This post/answer is useful
1
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Up-vote the answer, if it helps you! :)


Thank You
Raju Paladiya
-------------------------------------------------------
nopAccelerate - Faster, Reliable & Scalable nopCommerce

sales(at)nopaccelerate.com
http://www.nopAccelerate.com | http://www.xcellence-it.com
Posted: August 22, 2018 at 9:08 PM Quote #210930
This is the error message... The table names are unique

"Ambiguous match found."

"   at System.RuntimeType.GetPropertyImpl(String name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)\r\n   at System.Type.GetProperty(String name, BindingFlags bindingAttr)\r\n   at System.SharedTypeExtensions.GetPropertiesInHierarchy(Type type, String name)+MoveNext()\r\n   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)\r\n   at System.Reflection.PropertyInfoExtensions.FindSetterProperty(PropertyInfo propertyInfo)\r\n   at System.Reflection.PropertyInfoExtensions.IsCandidateProperty(PropertyInfo propertyInfo, Boolean needsWrite, Boolean publicOnly)\r\n   at Microsoft.EntityFrameworkCore.Metadata.Internal.MemberClassifier.FindCandidateNavigationPropertyType(PropertyInfo propertyInfo)\r\n   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.RelationshipDiscoveryConvention.GetNavigationCandidates(EntityType entityType)\r\n   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.RelationshipDiscoveryConvention.FindRelationshipCandidates(InternalEntityTypeBuilder entityTypeBuilder)\r\n   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.RelationshipDiscoveryConvention.DiscoverRelationships(InternalEntityTypeBuilder entityTypeBuilder)\r\n   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnEntityTypeAdded(InternalEntityTypeBuilder entityTypeBuilder)\r\n   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.RunVisitor.VisitOnEntityTypeAdded(OnEntityTypeAddedNode node)\r\n   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ConventionVisitor.VisitConventionScope(ConventionScope node)\r\n   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ConventionBatch.Run()\r\n   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalModelBuilder.Entity(TypeIdentity& type, ConfigurationSource configurationSource, Boolean throwOnQuery)\r\n   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalModelBuilder.Entity(Type type, ConfigurationSource configurationSource, Boolean throwOnQuery)\r\n   at Microsoft.EntityFrameworkCore.ModelBuilder.Entity[TEntity]()\r\n   at Microsoft.EntityFrameworkCore.ModelBuilder.ApplyConfiguration[TEntity](IEntityTypeConfiguration`1 configuration)\r\n   at Nop.Plugin.Fanolli.Journal.Data.FanolliJournalObjectContext.OnModelCreating(ModelBuilder modelBuilder)\r\n   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)\r\n   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)"

{System.Reflection.PropertyInfo GetPropertyImpl(System.String, System.Reflection.BindingFlags, System.Reflection.Binder, System.Type, System.Type[], System.Reflection.ParameterModifier[])}
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: August 22, 2018 at 9:19 PM Quote #210931
nopCommerce version ?
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Up-vote the answer, if it helps you! :)


Thank You
Raju Paladiya
-------------------------------------------------------
nopAccelerate - Faster, Reliable & Scalable nopCommerce

sales(at)nopaccelerate.com
http://www.nopAccelerate.com | http://www.xcellence-it.com
Posted: August 22, 2018 at 9:20 PM Quote #210932
latest 4.1
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: August 22, 2018 at 9:29 PM Quote #210933
Have you done this before? custom plugin with two tables? Could you share how you do it?
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: August 22, 2018 at 9:43 PM Quote #210934
Might be you missing register entity for table 2 in with dbContext in DependencyRegistrar file.


          // for table 1
            builder.RegisterType<EfRepository<DBTable1>>().As<IRepository<DBTable1>>()
                .WithParameter(ResolvedParameter.ForNamed<IDbContext>("your_db_context_name"))
                .InstancePerLifetimeScope();

            // for table 2
            builder.RegisterType<EfRepository<DBTable2>>().As<IRepository<DBTable2>>()
                .WithParameter(ResolvedParameter.ForNamed<IDbContext>("your_db_context_name"))
                .InstancePerLifetimeScope();
This post/answer is useful
2
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Up-vote the answer, if it helps you! :)


Thank You
Raju Paladiya
-------------------------------------------------------
nopAccelerate - Faster, Reliable & Scalable nopCommerce

sales(at)nopaccelerate.com
http://www.nopAccelerate.com | http://www.xcellence-it.com
Posted: August 23, 2018 at 10:21 AM Quote #210971
You can try this:

DependencyRegistrar;


private const string CONTEXT1_NAME = "nop_object_context_table1";
        private const string CONTEXT2_NAME = "nop_object_context_table2";


Then in Register;


//data context
            builder.RegisterPluginDataContext<Table1DataObjectContext>(CONTEXT1_NAME);
            builder.RegisterPluginDataContext<Table2DataObjectContext>(CONTEXT2_NAME);

            //override required repository with our custom context
            builder.RegisterType<EfRepository<Table1>>().As<IRepository<Table1>>()
                .WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT1_NAME))
                .InstancePerLifetimeScope();
            //override required repository with our custom context
            builder.RegisterType<EfRepository<Table2>>().As<IRepository<Table2>>()
                .WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT2_NAME))
                .InstancePerLifetimeScope();


Take a look at Pickup in Store Plugin DependencyRegistrar in Infrastructure folder.
This post/answer is useful
1
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Dia [email protected]

Please vote for my comment if it is helpful for you.
Posted: August 26, 2018 at 4:06 PM Quote #211112
dianoche wrote:
You can try this:

DependencyRegistrar;


private const string CONTEXT1_NAME = "nop_object_context_table1";
        private const string CONTEXT2_NAME = "nop_object_context_table2";


Then in Register;


//data context
            builder.RegisterPluginDataContext<Table1DataObjectContext>(CONTEXT1_NAME);
            builder.RegisterPluginDataContext<Table2DataObjectContext>(CONTEXT2_NAME);

            //override required repository with our custom context
            builder.RegisterType<EfRepository<Table1>>().As<IRepository<Table1>>()
                .WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT1_NAME))
                .InstancePerLifetimeScope();
            //override required repository with our custom context
            builder.RegisterType<EfRepository<Table2>>().As<IRepository<Table2>>()
                .WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT2_NAME))
                .InstancePerLifetimeScope();


Take a look at Pickup in Store Plugin DependencyRegistrar in Infrastructure folder.


That sure did help but now I am stuck on the OnModelCreating part... How do I pass more then one entity to the OnModelCreating. Any Idea?


protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.ApplyConfiguration(new Table1RecordMap());
            modelBuilder.ApplyConfiguration(new Table2RecordMap());
            base.OnModelCreating(modelBuilder);
        }
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: August 26, 2018 at 11:36 PM Quote #211118
mcselasvegas wrote:
You can try this:

DependencyRegistrar;


private const string CONTEXT1_NAME = "nop_object_context_table1";
        private const string CONTEXT2_NAME = "nop_object_context_table2";


Then in Register;


//data context
            builder.RegisterPluginDataContext<Table1DataObjectContext>(CONTEXT1_NAME);
            builder.RegisterPluginDataContext<Table2DataObjectContext>(CONTEXT2_NAME);

            //override required repository with our custom context
            builder.RegisterType<EfRepository<Table1>>().As<IRepository<Table1>>()
                .WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT1_NAME))
                .InstancePerLifetimeScope();
            //override required repository with our custom context
            builder.RegisterType<EfRepository<Table2>>().As<IRepository<Table2>>()
                .WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT2_NAME))
                .InstancePerLifetimeScope();


Take a look at Pickup in Store Plugin DependencyRegistrar in Infrastructure folder.

That sure did help but now I am stuck on the OnModelCreating part... How do I pass more then one entity to the OnModelCreating. Any Idea?


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


I am not sure if it is possible that way but I always create new DataObjectContext for each context. You will have 2 DataObjectContex File


//data context
            builder.RegisterPluginDataContext<Table1DataObjectContext>(CONTEXT1_NAME);//First One
            builder.RegisterPluginDataContext<Table2DataObjectContext>(CONTEXT2_NAME);//Second One
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Dia [email protected]

Please vote for my comment if it is helpful for you.
1 2 >
Premium support services
  • Dedicated premium support services provided by core developers are intended for persons who run mission critical websites, work on projects with tight deadlines, or want to get dedicated support.
Professional services
  • Want to open a new store? Want to take your store to the next level? Need a custom extension? We can customize nopCommerce to fit your store perfectly. Request a quote to get started.
eCommerce CONFERENCE 2019
Learn more