i 'm newbie to nopCommerce and i need to create a table , so i searched it and read a lot of duplicate content but i could not create table . the best of document about this is :
http://www.lateshtclick.com/blogpost/how-to-add-new-tables-to-nopcommerce
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");
}
}
I do it step by step but in the end I encountered a number of errors, and I did not get a table
please help me for create table in nopCommerce 3.9
thanks