I need help in adding extra product fields in database.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 years ago
Here is a  step-by-step tutorial for adding a new table to nopCommerce 1.9v

Adding a single table called "Widget" with no relationships to any other table.

There are basically 6 steps to this process:

1. Create the Widget table in the db.
2. Create the Widget class.
3. Update the db schema.
4. Update the object context.
5. Register for Managers Mappings.
6. Create new CRUD manager class.

Create the Widget table in the db.

1. Open SSMS, Select  New Table from NopCommerce Database.
2. Add the following column names and data types.  Be aware of case.
Id-->int-->not null-->mark as identity column
Name-->nvarchar(50)-->not null
Description-->nvarchar(200)-->not null
3. Save table name as "Widget".

Create Widget Class

1. Create new folder in Nop.BusinessLogic called "Widgets".
2. Create new class file called "Widget". The code should like below

using System;
using System.Collections.Generic;
using System.Text;

namespace NopSolutions.NopCommerce.BusinessLogic.Widgets.Widget
{
    /// <summary>
    /// Represents a widget
    /// </summary>
    public partial class Widget : BaseEntity
    {
        #region Constructor
        /// <summary>
        /// Creates a new instance of the Widget class
        /// </summary>
        public Widget()    
        {     }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the widget identifier
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// Gets or sets the name
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the description
        /// </summary>
        public string Description { get; set; }

        #endregion
    }
}

Update DB Schema

1. Open NopModel.edmx from Nop.BusinessLogic.Data
2. Right click on any open space in the model diagram and click 'Update Model From Database'.
3. A wizard will pop up and your db connection should already be setup.  If not then set it up. Click no and then Next.
4. A tree view of the nop db will appear.  Open the tables tree and ONLY select the Widget table you created in step 1.  Click Finish.
5. You should now see your table in the edmx diagram.
6. IMPORTANT!! Ensure that your table name and column names match your class names EXACTLY.  You will experience great pain if they are different.

Update Object Context

1. Open NopObjectContext.ObjectSets.cs from Nop.BusinessLogic.Data
2. After update the .edmx file ,automatically create a property in  NopObjectContext.ObjectSets.cs to create an object set of the widget class.
3.  Add a reference  using NopSolutions. NopCommerce.BuisinessLogic.Widgets; to NopObjectContext.ObjectSets.tt.
4.  Add a reference (using) to NopSolutions.NopCommerce. BusinessLogic.Widgets in NopObjectContext.ObjectSets.cs.
5. Automatically created property is shown below.

/// <summary>
        /// Gets an Widget instance that is used to query, add, modify, and delete objects of the specified entity type.
        /// </summary>
        public ObjectSet<Widget> Widgets
        {
            get
            {
                if ((_widgets == null))
                {
                    _widgets = CreateObjectSet<Widget>();
                }
                return _widgets;
            }
        }
        private ObjectSet<Widget> _widgets;

Register for Managers Mappings.

1. Open UnityDependencyResolver.cs from  Nop.BusinessLogic\Infrastructure
2.  Add a reference of the widget into UnityDependencyResolver.cs and also register for managers(services) mappings ,
  
          container.RegisterType<IWidgetService, WidgetService>(new UnityPerExecutionContextLifetimeManager());

Create New CRUD Manager Class

1. Create a Interface "IWidgetSevice.cs" in Nop.BusinessLogic.Widgets
1. Create a new class file called WidgetService.cs in Nop.BusinessLogic.Widgets
2. NOTE: This example does not include caching support.  You will need to determine if this is necessary based on your application needs.
3. Should declare the functions in IWidgetSevice.cs (the functions write in WidgetService.cs), show the examples below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NopSolutions.NopCommerce.BusinessLogic.Widgets
{
    public partial interface IWidgetService
    {
        Widget GetWidgetById(int widgetId);
        Widget AddWidget(string name, string description);
        void DeleteWidget(int widgetId);
        Widget UpdateWidget(string name, string description, int widgetId);        
    }
}

4. Design your CRUD operations to match your application needs.  Here are some examples below.

namespace NopSolutions.NopCommerce.BusinessLogic.Widgets
{
    public partial class WidgetService : IWidgetService
    {
        // Fields
        private readonly NopObjectContext _context;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="context"></param>
        public WidgetService(NopObjectContext context)
        {
            this._context = context;
        }

        //GET

        public Widget GetWidgetById(int widgetId)
        {
            if (widgetId == 0)
                return null;        
            var query = from w in _context.Widgets  
                        where w.Id == widgetId
                        select w;
            var widget = query.SingleOrDefault();
            return widget;
        }

        //INSERT

        public Widget AddWidget(string name, string description)
        {
            name = CommonHelper.EnsureMaximumLength(name, 50);
            description = CommonHelper.EnsureMaximumLength(description, 200);          
            var widget = _context.Widgets.CreateObject();
            widget.Name = name;
            widget.Description = description;                        
            _context.Widgets.AddObject(widget);
            _context.SaveChanges();

            return widget;
        }

        //UPDATE

        public Widget UpdateWidget(string name, string description,int widgetId)
        {
            name = CommonHelper.EnsureMaximumLength(name, 50);
            description = CommonHelper.EnsureMaximumLength(description, 200);

            var widget = GetWidgetById(widgetId);
            if (widget == null)
                 return null;          
            if (!_context.IsAttached(widget))
                 _context.Widgets.Attach(widget);
            widget.Name = name;
            widget.Description = description;
            _context.SaveChanges();
            return widget;
        }

        //DELETE

        public void DeleteWidget(int widgetId)
        {
            var widget = GetWidgetById(widgetId);
            if (widget == null)
                return;        
            if (!_context.IsAttached(widget))
                _context.Widgets.Attach(widget);
            _context.DeleteObject(widget);
            _context.SaveChanges();

        }
    }
}
13 years ago
Thanks webqa.  That was a great step-by-step.   It is very much appreciated ...
12 years ago
hemant wrote:
I am not sure why but i am getting this error on adding a new entity ProductSubtopic
exc = {"The ObjectStateManager does not contain an ObjectStateEntry with a reference to an object of type 'NopSolutions.NopCommerce.BusinessLogic.ProductTopics.ProductSubtopic'."}

I added another entity following the same steps it works fine, please help me out i am new at this


This happens at entry = context.ObjectStateManager.GetObjectStateEntry(entity); in  class Extensions.cs

Method isAttached
12 years ago
Sounds like an object context or dependency resolver issue.  Do the names in the object context class  match the properties in the edmx exactly?
12 years ago
This small tutorial is very helpful. Now I understand better how the software works in the background. Thank you!
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.