Extending Nop data model

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
10 years ago
opescode wrote:
I understand the thread is now so old, but I wonder if Nopcommerce have a solution for such case now!?


No, this has not been addressed.  There is a link to a work item for this, which is listed as proposed.  Make sure to upvote it to help get it into the list of items for an upcoming release.
10 years ago
Hello All,

I have the same Issue with Product table

Here is my Map Class
public class DealOfDayProductMap : EntityTypeConfiguration<DealOfDayProduct>
    {
        public DealOfDayProductMap()
        {
            this.ToTable("DealOfDayProduct");

            this.HasKey(dd => dd.Id);

            this.HasRequired(dd => dd.Product)
                        .WithMany()
                        .HasForeignKey(dd => dd.ProductId);
        }
    }


My DealOfDayProduct class

public partial class DealOfDayProduct : BaseEntity
    {
        public int ProductId { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public decimal Discount { get; set; }
        public virtual Product Product { get; set; }
        public bool? IsApproved { get; set; }
    }

But I Amd not able to get ProductId as foreign key to nopCommerce Table in 3.1

If is there any solution please let me know...
9 years ago
I'm having the same issue

 //Map the primary key
            HasKey(m => m.Id);
            Property(m => m.VendorId);
            Property(m => m.CustomerId);
            Property(m => m.CreatedUtc);

          
            //Mapping the follower table with vendoer table by vendor Id
            this.HasRequired(v => v.Vendor)
                .WithMany()
                .HasForeignKey(v => v.VendorId);

            //Mapping the follower table with Customer table by Customer Id
            this.HasRequired(c => c.Customer)
                .WithMany()
                .HasForeignKey(c => c.CustomerId);



Unable to determine the principal end of an association between the types 'Nop.Core.Domain.Orders.Order' and 'Nop.Core.Domain.Customers.RewardPointsHistory'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
9 years ago
SoftAIN wrote:
Could you share your plugin source code?

My plugin’s code:

--------------------------------------
Domain:
---------

    public class Action : BaseEntity
    {
        public virtual string Title { get; set; }
        public virtual string Description { get; set; }
        public virtual DateTime StartDate { get; set; }
        public virtual DateTime FinishDate { get; set; }
        public virtual decimal? Amount { get; set; }

        public virtual ICollection<ActionTargetProductVariant> TargetProducts { get; set; }
    }

    public class ActionTargetProductVariant: BaseEntity
    {
        public virtual int ActionId { get; set; }
        public virtual int ProductVariantId { get; set; }
        public virtual Action Action { get; set; }
        public virtual ProductVariant ProductVariant { get; set; }
        public virtual decimal? Price { get; set; }
    }

Data:
------
    public partial class ActionMap : EntityTypeConfiguration<Nop.Plugin.SoftAIN.Action.Domain.Action>
    {
        public ActionMap()
        {
            this.ToTable("Action");
            this.HasKey(a => a.Id);

            this.Property(a => a.Title).IsRequired().HasMaxLength(256);
            this.Property(a => a.Description).IsRequired();
            this.Property(a => a.StartDate).IsRequired();
            this.Property(a => a.FinishDate).IsRequired();
            this.Property(a => a.Amount);
        }
    }

   public partial class ActionTargetProductVariantMap :   EntityTypeConfiguration<Nop.Plugin.SoftAIN.Action.Domain.ActionTargetProductVariant>
    {
        public ActionTargetProductVariantMap()
        {
            this.ToTable("Action_TargetProductVariant");
            this.HasKey(atp => atp.Id);

            this.Property(atp => atp.ActionId);
            this.Property(atp => atp.ProductVariantId);

            this.HasRequired(atp => atp.Action)
                .WithMany(a => a.TargetProducts)
                .HasForeignKey(atp => atp.ActionId);

            this.HasRequired(atp => atp.ProductVariant);
        }

    public class ActionObjectContext : DbContext, IDbContext
    {
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new ActionMap());
            modelBuilder.Configurations.Add(new ActionTargetProductVariantMap());
        
            modelBuilder.Conventions.Remove<IncludeMetadataConvention>();

            base.OnModelCreating(modelBuilder);
        }


Registration:
--------------
    public class DependencyRegistrar : IDependencyRegistrar
    {
        private const string CONTEXT_NAME = "nop_object_context_softain_action";

        public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder)
        {
            //Load custom data settings
            var dataSettingsManager = new DataSettingsManager();
            DataSettings dataSettings = dataSettingsManager.LoadSettings();

            //Register custom object context
            builder.Register<IDbContext>(c => RegisterIDbContext(c, dataSettings)).Named<IDbContext>(CONTEXT_NAME).InstancePerHttpRequest();
            builder.Register(c => RegisterIDbContext(c, dataSettings)).InstancePerHttpRequest();

            
            builder.RegisterType<EfRepository<SoftAIN.Action.Domain.Action>>().As<IRepository<SoftAIN.Action.Domain.Action>>().WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT_NAME)).InstancePerHttpRequest();
            builder.RegisterType<EfRepository<SoftAIN.Action.Domain.ActionTargetProductVariant>>().As<IRepository<SoftAIN.Action.Domain.ActionTargetProductVariant>>().WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT_NAME)).InstancePerHttpRequest();
        }

-----------------------------------------------------

I don’t know why my own named DbContext depend on the common one, but it does.

Btw, these changes fix the problem:

----------------------------------------------------------------------------------------------------------

Order (collection instead one property):
---------------------------------------------
         public virtual ICollection<RewardPointsHistory> RedeemedRewardPointsEntities { get; set; }
        
        public RewardPointsHistory RedeemedRewardPointsEntry
        {
            get { return RedeemedRewardPointsEntities == null ? null : RedeemedRewardPointsEntities.FirstOrDefault(); }
            set { }
        }

RewardPointsHistory (added property)
-------------------------------------------

public virtual int UsedWithOrderId { get; set; }

RewardPointsHistoryMap (moving to WithMany):
------------------------------------------------------

         this.HasRequired(rph => rph.UsedWithOrder)
                .WithMany(o => o.RedeemedRewardPointsEntities )
                .HasForeignKey(rph => rph.UsedWithOrderId);

And, in the database, in table RewardPointsHistory I replaced UsedWithOrder_Id by UsedWithOrderId to follow the conditions (it prevernts creating a new FK by EF).

I understand, that the fixes above is a workaround, not the solution, so, maybe, there is a better way?


Hello Can you let me know what exactly I need to change in my code to Extend data model with reference table because I am also getting the same Issue.
9 years ago
I have the same Issue! I change method OnModelCreating and this work

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
            .Where(type => !String.IsNullOrEmpty(type.Namespace))
            .Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
            foreach (var type in typesToRegister)
            {
                dynamic configurationInstance = Activator.CreateInstance(type);
                modelBuilder.Configurations.Add(configurationInstance);
            }

            base.OnModelCreating(modelBuilder);
        }
9 years ago
"In custom context classes we cannot reference previously existing entities because those types are already associated to another object context. That is also why we do not have complex navigation properties in our tracking record."
6 years ago
Hi.
It seems to be old problem and yet I have same issue on version 3.90.
I have an extension of OrderItem class. While getting list of order items I'm trying to eliminate ones belonging to deleted order:

query = query.Where(oi => this._orderService.GetOrderItemById(oi.OrderItemId).Order.Deleted);


At this point I get same exception.
As you can see I'm not referencing Order instance directly. Instead I'm trying to get one via IOrderService. But still get same problem.

I tried to use this code:


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
                .Where(type => !string.IsNullOrEmpty(type.Namespace))
                .Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
            foreach (var type in typesToRegister)
            {
                dynamic configurationInstance = Activator.CreateInstance(type);
                modelBuilder.Configurations.Add(configurationInstance);
            }
            modelBuilder.Configurations.Add(new OrderItemMap());

            base.OnModelCreating(modelBuilder);
        }


but there was no luck.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.