Extend product entities from custom plugin gives an error

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

I'm trying to extend product entities from my plugin, and I want to add one custom field to the product entity class.

As per we know, Product entity class is partial class, so I've created one partial class in my plugin.

    public partial class Product
    {
        public string TestProperty { get; set; }
    }


And trying to get this to the controller.

var product = _productService.GetProductById(1);


product.TestProperty ="testProperty";


Now I'm wondering for TestProperty, sometimes it appears and sometimes it gives an error "TestProperty Could not found, missing ref".

Second thing,

When I Add product to update service, it's not showing any error. But at a time of build the project it's give an error.

 _productService.UpdateProduct(product);


Error 1 'Nop.Core.Domain.Catalog.Product' does not contain a definition for 'TestProperty' and no extension method 'TestProperty' accepting a first argument of type 'Nop.Core.Domain.Catalog.Product' could be found (are you missing a using directive or an assembly reference?)

Am I missing something?

P.S: I've referred Proposal of a modified nopCommerce architecture to extend entities
7 years ago
divyang16 wrote:
Hello,
...


Hi,

unfortunately, you cannot add new properties to existing entities from a plugin.

The partial classes work in a different way. All partial class(es) must be in the same assembly, because they are put together at compile-time, not at runtime. This means that you can't have, for example, Product partial class in a plugin.

The change in architecture from the link that you mentioned above basically gives you the ability to put your modification in your own files instead of modifying the nopCommerce core code. But your partial classes must be defined in the same assembly as the entities.

Hope this helps!

Regards,
Hristian
7 years ago
Hi Hristian,

Thank you! yeah, it helped.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.