Getting Error when calling Uninstall method. [v3.1]

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
10 yıl önce
I am trying to create a plugin based on the tutorial given here. Things go wrong when calling Install or Uninstall and data context class is initialized. I am getting this error:

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. 

My models are as follows:

    public class MyProduct : BaseEntity
    {
        public int CustomerId { get; set; }
        public Customer Customer { get; set; }

        public int ProductId { get; set; }

        public string ProductName { get; set; }

        public int NotesCount { get; set; }

        public virtual ICollection<MyProductNote> Items { get; set; }

        public MyProduct()
        {
            Items = new HashSet<MyProductNote>();
        }
    }


    public class MyProductNote : BaseEntity
    {
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string LastName { get; set; }
        public string Note { get; set; }

        public int MyProductId { get; set; }
        public virtual MyProduct MyProduct { get; set; }

        public string FullName
        {
            get
            {
                return FirstName + (string.IsNullOrWhiteSpace(MiddleName) ? "" : (" " + MiddleName)) + " " + LastName;
            }
        }
    }


The mapping EntityTypeConfiguration classes are:


    public class MyProductMap : EntityTypeConfiguration<MyProduct>
    {
        public MyProductMap()
        {
            ToTable("LTMyProduct");

            HasKey(x => x.Id);

            Property(x => x.CustomerId).IsRequired();
            Property(x => x.ProductId).IsRequired();

            Property(x => x.ProductName).IsUnicode().IsVariableLength().HasMaxLength(250).IsOptional();

            HasMany(x => x.Items);
        }
    }

    public class MyProductNoteMap : EntityTypeConfiguration<MyProductNote>
    {
        public MyProductNoteMap()
        {
            ToTable("LTMyProductNote");

            HasKey(x => x.Id);

            Property(x => x.FirstName).HasMaxLength(50).IsRequired().IsUnicode().IsVariableLength();
            Property(x => x.MiddleName).HasMaxLength(50).IsUnicode().IsVariableLength();
            Property(x => x.LastName).HasMaxLength(50).IsRequired().IsUnicode().IsVariableLength();
            Property(x => x.Note).IsVariableLength().HasMaxLength(1024).IsUnicode();
            Property(x => x.MyProductId).IsRequired().HasColumnType("int");
        }
    }

When DataContext class's OnModelCreating method is called:

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new MyProductMap());
            modelBuilder.Configurations.Add(new MyProductNoteMap());
            base.OnModelCreating(modelBuilder);
        }

The line base.OnModelCreating(modelBuilder); is causing the issue.

It gives aforementioned exception. Note that I am not referring to any NopCommerce base class except the BaseEntity.
10 yıl önce
realnavnit wrote:
I am trying to create a plugin based on the tutorial given here. Things go wrong when calling Install or Uninstall and data context class is initialized. I am getting this error:

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. 

My models are as follows:

    public class MyProduct : BaseEntity
    {
        public int CustomerId { get; set; }
        public Customer Customer { get; set; }

        public int ProductId { get; set; }

        public string ProductName { get; set; }

        public int NotesCount { get; set; }

        public virtual ICollection<MyProductNote> Items { get; set; }

        public MyProduct()
        {
            Items = new HashSet<MyProductNote>();
        }
    }


    public class MyProductNote : BaseEntity
    {
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string LastName { get; set; }
        public string Note { get; set; }

        public int MyProductId { get; set; }
        public virtual MyProduct MyProduct { get; set; }

        public string FullName
        {
            get
            {
                return FirstName + (string.IsNullOrWhiteSpace(MiddleName) ? "" : (" " + MiddleName)) + " " + LastName;
            }
        }
    }


The mapping EntityTypeConfiguration classes are:


    public class MyProductMap : EntityTypeConfiguration<MyProduct>
    {
        public MyProductMap()
        {
            ToTable("LTMyProduct");

            HasKey(x => x.Id);

            Property(x => x.CustomerId).IsRequired();
            Property(x => x.ProductId).IsRequired();

            Property(x => x.ProductName).IsUnicode().IsVariableLength().HasMaxLength(250).IsOptional();

            HasMany(x => x.Items);
        }
    }

    public class MyProductNoteMap : EntityTypeConfiguration<MyProductNote>
    {
        public MyProductNoteMap()
        {
            ToTable("LTMyProductNote");

            HasKey(x => x.Id);

            Property(x => x.FirstName).HasMaxLength(50).IsRequired().IsUnicode().IsVariableLength();
            Property(x => x.MiddleName).HasMaxLength(50).IsUnicode().IsVariableLength();
            Property(x => x.LastName).HasMaxLength(50).IsRequired().IsUnicode().IsVariableLength();
            Property(x => x.Note).IsVariableLength().HasMaxLength(1024).IsUnicode();
            Property(x => x.MyProductId).IsRequired().HasColumnType("int");
        }
    }

When DataContext class's OnModelCreating method is called:

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new MyProductMap());
            modelBuilder.Configurations.Add(new MyProductNoteMap());
            base.OnModelCreating(modelBuilder);
        }

The line base.OnModelCreating(modelBuilder); is causing the issue.

It gives aforementioned exception. Note that I am not referring to any NopCommerce base class except the BaseEntity.


Are you trying to link entities from your plugin's Context to nopCommerce's Context? As far as I know, this is not technically allowed.
10 yıl önce
This is all the code that is. Rest the dependency registration and other stuff is just as explained in this tutorial. I will not directly access the Nop  context. I am planning to use the services like ICustomerService, IRepository<Product>, IWorkContext etc. to access Nop objects. But it does not goes till there even. The moment I hit base.OnModelCreating(modelBuilder); line it throws the exception.
10 yıl önce
Well tried following line as suggested here and it works.

 modelBuilder.Entity<RewardPointsHistory>().HasRequired(p => p.UsedWithOrder).WithOptional();


But now getting another error:
There is already an object named 'Address_Country' in the database.
Could not create constraint. See previous errors.
9 yıl önce
realnavnit, I know it's been a while since this post was made, but did you ever find a resolution? I am running into the same error but during a Plugin install.  

"There is already an object named 'Address_Country' in the database.
Could not create constraint. See previous errors."
9 yıl önce
"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."
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.