posted by  on  

How to add new tables to NopCommerce without deleting my data from database. Step by step explanation for how to add new table on NopCommerce.

Open source code on visual studio then follow the below steps

Step 1

Open this path \Libraries\Nop.Core\Domain and create one folder under this path and folder name is "User".

Create the Entity class and give name "UserMaster.cs".

public partial class UserMaster : BaseEntity
{
    public string Name { get; set; }

    public int Age { get; set; }

    public string Address { get; set; }
}

Step 2

Open this path \Libraries\Nop.Data\Mapping and create one folder under this path and folder name is "User".

Create a Mapping class which bind class to Database table and give name "UserMasterMap.cs".

public partial class UserMasterMap : NopEntityTypeConfiguration<UserMaster>
{
    public UserMasterMap()
    {
        this.ToTable("UserMaster"); // Provide Table Name
        this.HasKey(um => um.Id);
        this.Property(um => um.Name).IsRequired();
        this.Property(um => um.Age).IsRequired();
    }
}

Step 3

Open this path ~\Nop.Admin\Models and create one folder under this path and folder name is "User".

Create a Model class for MVC and give name "UserMasterModel.cs". (Same for Nop.Web)

public partial class UserMasterModel : BaseNopEntityModel
{
    [NopResourceDisplayName("Admin.UserMaster.Fields.Name")]
    public string Name { get; set; }

    [NopResourceDisplayName("Admin.UserMaster.Fields.Age")]
    public int Age { get; set; }

    [NopResourceDisplayName("Admin.UserMaster.Fields.Address")]
    public string Address { get; set; }
}

Step 4

Open this path ~\Nop.Admin\Validators and create one folder under this path and folder name is "User".

Create a Validator class for MVC and give name "UserMasterValidator.cs". (Same for Nop.Web)

public partial class UserMasterValidator : BaseNopValidator<UserMasterModel>
{
    public UserMasterValidator(ILocalizationService localizationService, IDbContext dbContext)
    {
        RuleFor(x => x.Name).NotEmpty().WithMessage(localizationService.GetResource("Admin.UserMaster.Fields.Name.Required"));

        SetStringPropertiesMaxLength<UserMaster>(dbContext);
    }
}

Step 5

Open this path ~\Nop.Admin\Infrastructure.

Open AutoMapperConfiguration.cs class and write below code for Mapping Model to Entity and Entity to Model. (Same for Nop.Web)

// UserMaster, you can ignore properties here
cfg.CreateMap<UserMaster, UserMasterModel>()
    .ForMember(dest => dest.CustomProperties, mo => mo.Ignore());
cfg.CreateMap<UserMasterModel, UserMaster>();

Step 6

Open this path ~\Nop.Admin\Extensions.

Open MappingExtensions.cs class and write below code for Mapping Model to Entity and Entity to Model. (Same for Nop.Web)

#region UserMaster

//UserMaster
public static UserMasterModel ToModel(this UserMaster entity)
{
    return entity.MapTo<UserMaster, UserMasterModel>();
}

public static UserMaster ToEntity(this UserMasterModel model)
{
    return model.MapTo<UserMasterModel, UserMaster>();
}

public static UserMaster ToEntity(this UserMasterModel model, UserMaster destination)
{
    return model.MapTo(destination);
}

#endregion

Step 7

Open this path ~\Libraries\Nop.Services and create one folder under this path and folder name is "User".

Create a interface and give name "IUserMasterService.cs". (Same for Nop.Web)

public partial interface IUserMasterService
{
    void InsertUser(UserMaster userMaster);

    void UpdateUser(UserMaster userMaster);

    UserMaster GetUserById(int userId);

    void DeleteUser(UserMaster userMaster);  
}

Create a interface and give name "UserMasterService.cs". (Same for Nop.Web)

This method is only for insert new record in UserMaster Table. Here you can create methods whatever you want.

public partial class UserMasterService : IUserMasterService
{
    #region Fields

    private readonly IRepository<UserMaster> _userMasterRepository;
    private readonly IEventPublisher _eventPublisher;

    #endregion

    #region Ctor

    public UserMasterService(IRepository<UserMaster> userMasterRepository,
        IEventPublisher eventPublisher)
    {
        this._userMasterRepository = userMasterRepository;
        this._eventPublisher = eventPublisher;
    }

    #endregion

    public virtual void InsertUser(UserMaster userMaster)
    {
        if (userMaster == null)
            throw new ArgumentNullException("userMaster");

        _userMasterRepository.Insert(userMaster);

        //event notification
        _eventPublisher.EntityInserted(userMaster);
    }
}

Step 8

Open this path ~\Nop.Web.Framework and open DependencyRegistrar.cs and register your service and interface like this

// UserMaster
builder.RegisterType<UserMasterService>().As<IUserMasterService>().InstancePerLifetimeScope();

Step 9

Create table in NopCommerce database and give name "UserMaster". 

Table structure like this

NopCommerce UserMaster Table

Note: Manually create table in database is necessary.

Step 10

Create new controller and give name "UserMasterController.cs"

public partial class UserMasterController : BaseAdminController
{
    #region Fields

    private readonly IUserMasterService _userMasterService;

    #endregion

    #region Constructors

    public UserMasterController(IUserMasterService userMasterService)
    {
        this._userMasterService = userMasterService;
    }

    #endregion

    public ActionResult Create()
    {
        if (!_permissionService.Authorize(StandardPermissionProvider.ManageUserMaster))
            return AccessDeniedView();

        var model = new UserMasterModel();

        return View(model);
    }

    [HttpPost, ParameterBasedOnFormName("save-continue", "continueEditing")]
    public ActionResult Create(UserMasterModel model, bool continueEditing)
    {
        if (!_permissionService.Authorize(StandardPermissionProvider.ManageUserMaster))
            return AccessDeniedView();

        if (ModelState.IsValid)
        {
            var userMaster = model.ToEntity();
            _userMasterService.InsertUser(userMaster);
        }
        return RedirectToAction("List");
    }
}

Create View for action method.

Build your project and run application.

Lateshtclick Technology Blog
Tags NopCommerce MVC
posted by Latesht Click Latesht Click

Comments
G

Gary

 11/21/2016

This was really helpful. Thanks for the detailed step by step guide.

a

a.waheed

 6/11/2017

It was very detailed. You have covered nopCommerce part very well. It would be great if you explain the view creation part as well. If we go for 'Add View' by right clicking on the action. Which options we have to opt while creating views. Share source code if you have of UserMaster create view.

a

a.waheed

 6/11/2017

It was very detailed. You have covered nopCommerce part very well. It would be great if you explain the view creation part as well. If we go for 'Add View' by right clicking on the action. Which options we have to opt while creating views. Share source code if you have of UserMaster create view.

E

EE

 6/13/2017

Thanks awesome tutorial and very helpfull

E

EE

 6/13/2017

Thanks awesome tutorial and very helpfull

E

EE

 6/13/2017

Thanks awesome tutorial and very helpfull

E

EE

 6/13/2017

Thanks awesome tutorial and very helpfull

L

LBor

 9/2/2017

Hi , thank you 1item: in step 5 you said same for nop.web but nop.web have not this file (V3.9) ?

z
z
z
z
z
z
T

Test Star

 11/3/2017

Hi , thank you, but its not working here, so please help me, I don't understand step 7 and step 9.

k

krishtopher

 8/21/2018

Great Tutorial

K

Krisi

 11/15/2018

Is this tutorial valid for nopCommerce 3.9?



Leave a Reply

Feel free to contribute!
Comment:
Name*:
Email*: