Add table with references to existing entities

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

I am currently developping a plugin and i would like to extend the product entity and adding a relationship with a new table.

Let's imagine that i want to add a new relationship between Product table and a new entity named ProductRentalPeriod for example.

What is the best way to implement this relationship ?

- Solution 1: Update and add directly my new table and relationship in Nop.Core. In that case, it seems that I can use Entity Framework to create relationship. However, it forces me to modify Nop.Core and I would like to avoid it.

- Solution 2: Add my entity in my plugin and using a new db context in my plugin. I prefer this solution because i can put all of my code outside of Nop.Core project.
However it seems that in that case, I cannot add relationship between the Product entity and the new entity named ProductRentalPeriod.

What do you advise ? What are the pros and cons ?

Thanks a lot
5 years ago
Hi vans63,

2nd solution is best for you

In the 1st solution you have to customize the nopcommerce code , so it can be wide changes and effect to others.
and in 2nd solution

you can create a table for mapping product Id into your custom table and by that ID you can access all default functionality of product , so by this you can get your solution

Thanks
5 years ago
Ok thanks a lot for the information !
4 years ago
Hello,

Did anyone find a solution for the second option? I am exactly in the same situation.
4 years ago
If you do not want to touch the core of Nop and only extending the functionality from your plugin, which I also had experienced early, you need to manually control the relationship between your entities and Nop entities, which actually is a better idea but will take a lot of time to do that.

For example, in my case, I want to reference the product from my entity. I store the Nop product Id within my entity. When doing CRUD operations within my entity, I check to make sure the Id is correct and already existed. When doing work on Nop Product entity, I hook into their entity event life-cycles and either remove my entity or update it. Basically you would have to handle by yourself and not let the db handle the relations.

Also a note: Since the plugin db context is a different context than the nop db context, you won't be able to to join from two different contexts. What I had done so far is to project the result into a List and use List query to filter the result. It will affect the performance but that is the only way to do join in two different db contexts.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.