Extending entities in plugin

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 years ago
Hello.

I'm having difficulties extending with plugin default nopCommerce 3.7. entities like Country, Customer while extended Picture works fine.
I've tried different solutions and none of them were applicable to be used in plugin without touching source code. I have existing nopCommerce database (upgraded from nop 1.9 to 3.7) with several custom tables. So I need to implement all of them, including additional properties defining one-to-many, many-to-many and even one-to-one relations.

How I extend Picture:
1) created additional column on Picture table
2) inherited PictureExt from default Picture

public partial class PictureExt : Picture
{
    /// <summary>
    /// Gets or sets the ExternalUrl
    /// </summary>
    public virtual string ExternalUrl { get; set; }
}

3) fully copied PictureMap constructor to my PictureExtMap constructor

public class PictureMap : NopEntityTypeConfiguration<PictureExt>
{
    public PictureMap()
    {
        this.ToTable("Picture");
        this.HasKey(p => p.Id);
        this.Property(p => p.PictureBinary).IsMaxLength();
        this.Property(p => p.MimeType).IsRequired().HasMaxLength(40);
        this.Property(p => p.SeoFilename).HasMaxLength(300);
        this.Property(p => p.ExternalUrl).IsOptional();
   }
}

And it works. I can access PictureExt objects, and the field ExternalUrl will contain value.
This steps also worked for MyCustomCurrency extended from Currency.

Then I did the same for Country. I just need extended entity to implement some different relations later.
1) inherited Country

public partial class MyCustomCountry : Country
{
    
}

2) fully copied CountryMap constructor to my MyCustomCountryExtMap constructor

public partial class MyCustomCountryMap : NopEntityTypeConfiguration<MyCustomCountry>
{
    public MyCustomCountryMap()
    {
        this.ToTable("Country");
        this.HasKey(c => c.Id);
        this.Property(c => c.Name).IsRequired().HasMaxLength(100);
        this.Property(c => c.TwoLetterIsoCode).HasMaxLength(2);
        this.Property(c => c.ThreeLetterIsoCode).HasMaxLength(3);
    }
}

And it's not working... It shows an error: "Invalid column name 'Discriminator'".
Can someone will what is the major difference in this 2 examples? Why can I inherit Picture and Currency and can't inherit country and customer? And how i extend Country and Customer classes from a plugin?
4 years ago
Any answer please ?
4 years ago
I think its happening because of model relationships.
Also sql context in plugins are different from main app sql context then you can't use them in your plugin.
You should redefine all properties in plugin model without any relationship to main sql data models.
4 years ago
radhouene.laouini wrote:
Any answer please ?


It's a behavior of dotnet framework, not related to nopCommerce. Dotnet does not allow 2 partial classes referring to the same class in two different projects. After compilation, all partial classes referred to same class become single non-partial class.
4 years ago
public partial class CountryMap : NopEntityTypeConfiguration<MyCustomCountry>
{
    public CountryMap()
    {
        this.ToTable("Country");
        this.HasKey(c => c.Id);
        this.Property(c => c.Name).IsRequired().HasMaxLength(100);
        this.Property(c => c.TwoLetterIsoCode).HasMaxLength(2);
        this.Property(c => c.ThreeLetterIsoCode).HasMaxLength(3);
    }
}

try this one. may be this work
3 years ago
Hi _den
Have you got any solution to this issue?

And I have some more questions from your post. Kindly clarify.
1. Picture and Currency changes - have you done the changes in a separate plugin project? If yes could you share the steps?
2. Extending the database entity will expect a column with a name 'discriminator' in respective database table. This is the EF behaviour in inheritance. Then how your picture and currency class was working with this inheritance?
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.