Please note that this article applies to nopCommerce 1.80 only.
Since nopCommerce swapped out it’s existing provider based data access layer with the entity framework, there have been quite a few posts on the nopCommerce forums regarding how to extend nopCommerce.
In this post I am going to cover two of the most common requests
1) How to add a new property to an existing entity
2) How to add a new entity
Adding a new property to an existing entity
The process for this is very simple.
- Add your new property to the relevant class
- Update repository methods (insert/update)
- Add the field to your database table
- Update the Entity Framework model (edmx file)
In this example, I’m going to add a new property “Stylesheet” to my categories, that allows me to assign a specific CSS style sheet to each category.
So to begin with we should add our property. Rather than adding the property to the existing Category.cs file, I recommend you keep your extensions separate. Since the entity classes in nopCommerce are all partial classes, we can put our extension in a separate file.

We create our Category extension class in our extensions directory. We need to make sure that it sits in the same namespace as the existing category class so that the two are compiled together:
namespace NopSolutions.NopCommerce.BusinessLogic.Categories
{
///
/// Represents a category
///
public partial class Category : BaseEntity {
public string Stylesheet { get; set; }
}
}
Now that we have added our new property, we need to update the repository (or manager) insert and update methods accordingly (in the “adding a new entity” example, you will see my preferred approach for doing this).
We open up CategoryManager and find the insert and update methods, changing the method signatures like so:
public static Category InsertCategory(string name, string description,
int templateId, string metaKeywords, string metaDescription, string metaTitle,
string seName, int parentCategoryId, int pictureId,
int pageSize, string priceRanges, bool showOnHomePage, bool published, bool deleted,
int displayOrder, DateTime createdOn, DateTime updatedOn, string stylesheet) { ... }
public static Category UpdateCategory(int categoryId, string name, string description,
int templateId, string metaKeywords, string metaDescription, string metaTitle,
string seName, int parentCategoryId, int pictureId,
int pageSize, string priceRanges, bool showOnHomePage, bool published, bool deleted,
int displayOrder, DateTime createdOn, DateTime updatedOn, string stylesheet) { .... }
Note the additional variable “stylesheet”.
Of course now that we have changed these methods we need to update any code that references them. The easiest way of doing this is to right click the method and select “Find all references” from the context menu:

In this case we just need to update the CategoryInfo control used in administration (we will add a new textbox for our stylesheet property and use that to set the property).
With that done our application should compile (F6). Now it’s time to update our Entity Framework model.
We need to add our new field to the Nop_Category table. You can do this in Visual Studio, or (as I prefer) using SQL Server Management Studio:

Note that I am setting “Allow Nulls” to true. You will need to do this if the table already contains data. If you do not want to allow nulls you will need to run an update script to set a default value for your new property for all existing records.
With our database changes made, open up NopModel.edmx (inside Nop.BusinessLogic/Data).
Right click on the designer surface and select “Update Model from Database”. If it’s the first time you have done this you may need to set up the connection to your database:

With that done, you should see our new “Stylesheet” property on the category entity:

Note – the entity framework table mapping is case sensitive. If the case differs between entity and database (e.g. CategoryId <> CategoryID) you need to right click on the entity property within the edmx designer and select “Table Mapping”. You can then ensure that your properties are mapped correctly:

Now that we have updated our entity framework model, we need to return to our Insert and Update methods in CategoryManager and persist our “stylesheet” variable:

Now let’s look at adding a new entity.
Adding a new entity
In this example I am going to add a new “Banner” entity to nopCommerce since I would like to manage my site’s promotional banners from the nopCommerce administration interface.
I prefer to start with my domain, so let’s create a new class “Banner” and put it in our extensions directory.
namespace NopSolutions.NopCommerce.BusinessLogic.Extensions {
public class Banner {
public int BannerId { get; set; }
public string Name { get; set; }
public string Content { get; set; }
}
}
As you can see, our class is quite simple.
Now let’s add our new Banner table to the nopCommerce database. Personally I don’t like prefixing my table names (you should use schemas for that) but I will use the existing convention in this example i.e. “Nop_Banner”:

Note that my database fields are named slightly differently to the properties on our class. We will need to update the mappings after updating our entity framework model.
Open up NopModel.edmx, right click on the designer and select “Update Model from Database”.
On the “Add” tab, we select the new table and click “Finish”:

If you scroll up to the top left of the designer you will see the Entity Framework entity that has been created:

Let’s rename this to “Banner” (since that’s what our class is called) and rename our properties to match those on our class:

Since we are not using the entity framework generated object context in nopCommerce, we need to update our NopObjectContext to include our new entity. Again, since this is a partial class, we can stick it in our “Extensions” directory:
namespace NopSolutions.NopCommerce.BusinessLogic.Data
{
/// <summary>
/// Represents a nopCommerce object context
/// </summary>
public partial class NopObjectContext : ObjectContext {
private ObjectSet<Banner> _banners;
public ObjectSet<Banner> Banners {
get {
if ((_banners == null))
_banners = CreateObjectSet<Banner>();
return _banners;
}
}
}
}
This gives our object context a “Banners” property referencing the banner records in our database.
All that’s left to do is create our repository class “BannerManager”. I take a more domain driven approach than the existing manager classes in nopCommerce. We just have one method for update and insert and rather than passing in a variable for each property, we just take in the object itself. When it comes to making changes to our domain model this makes like much easier (if you’ve ever added any properties to Order, you will know what I’m talking about).
Our complete BannerManager class can be seen below.
namespace NopSolutions.NopCommerce.BusinessLogic.Extensions {
public class BannerManager {
public Banner GetBannerById(int bannerId) {
var db = ObjectContextHelper.CurrentObjectContext;
return db.Banners.SingleOrDefault(x => x.BannerId == bannerId);
}
public IList<Banner> GetAllBanners() {
var db = ObjectContextHelper.CurrentObjectContext;
return db.Banners.OrderBy(x => x.Name).ToList();
}
public void Save(Banner banner) {
var db = ObjectContextHelper.CurrentObjectContext;
if (banner.BannerId == 0) {
// new banner
db.Banners.AddObject(banner);
} else {
// existing banner
if (!db.IsAttached(banner))
db.Banners.Attach(banner);
}
db.SaveChanges();
}
}
}
I’m not doing any caching here. You can look at one of the existing manager classes for an example of that.
As you can see, entity framework has made extending nopCommerce much easier.
Have fun!