Here is a quick and dirty step-by-step tutorial for adding a new table to
nopCommerce 1.8. I have a video series that is half done and that I keep promising but alas I just haven't had time to finish it. For brevity sake I will condense this tutorial into more broad concepts. This will mean the developer will have to read between the lines a bit and rely on good ole' trial and error. Ok, lets start.
For this example we will be adding a single table called "Widget" with no relationships to any other table. This is to simplify the tutorial. I can add a more complex example with relationships and multiple tables in another tutorial.
There are basically 5 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. Create new CRUD manager class.
Create Widget Table in DB1. Open SQL management server or any other editing program and right click the tables tab under your nop db tree and select New Table.
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 Class1. Create new folder in Nop.BusinessLogic called "Widgets".
2. Create new class file called "Widget". The code should like 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 Ctor
/// <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 Schema1. 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 Context1. Open NopObjectContext.cs from Nop.BusinessLogic.Data
2. Add a reference (using) to NopSolutions.NopCommerce.BusinessLogic.Widgets.
3. Under the Object sets region add the following code.
/// <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;
NOTE: If you don't have intellisense support then you missed something or you need to build the project.
Create New CRUD Manager Class1. Create a new class file called WidgetManager.cs in Nop.BusinessLogic.Widgets
2. Reference (using) NopSolutions.NopCommerce.BusinessLogic.Data. NOTE: This example does not include caching support. You will need to determine if this is necessary based on your application needs.
3. Design your CRUD operations to match your application needs. Here are some examples below.
GET
public static Widget GetWidgetById(int widgetId)
{
if (widgetId == 0)
return null;
var context = ObjectContextHelper.CurrentObjectContext;
var query = from w in context.Widgets
where w.WidgetId == widgetId
select w;
var widget = query.SingleOrDefault();
return widget;
}
INSERT
public static Widget AddWidget(string name, string description)
{
name = CommonHelper.EnsureMaximumLength(name, 50);
desc = CommonHelper.EnsureMaximumLength(value, 200);
var context = ObjectContextHelper.CurrentObjectContext;
var widget = context.Widgets.CreateObject();
widget.Name = name;
widget.Description = desc;
context.Settings.AddObject(widget);
context.SaveChanges();
return widget;
}
UPDATE
public static Widget UpdateWidget(string name, string description)
{
name = CommonHelper.EnsureMaximumLength(name, 50);
desc = CommonHelper.EnsureMaximumLength(value, 200);
var widget = GetWidgetById(widgetId);
if (widget == null)
return null;
var context = ObjectContextHelper.CurrentObjectContext;
if !(context.IsAttached(widget))
context.Widgets.Attach(widget);
widget.Name = name;
widget.Description = desc;
context.SaveChanges();
return widget;
}
DELETE
public static void DeleteWidget(int widgetId)
{
var widget = GetWidgetById(widgetId);
if (widget == null)
return;
var context = ObjectContextHelper.CurrentObjectContext;
if (!context.IsAttached(widget))
context.Settings.Attach(widget);
context.DeleteObject(widget);
context.SaveChanges();
}
Finally. Build your project and reference the widgets assembly where ever you need to access your widget entity.
A couple of notes:
1. This is a very simplistic example. However, if you can get this to work (which you should have no problem doing) you will be able to customize this to meet your needs.
2. Ensure your have followed all of the above steps EXACTLY. Don't skip over steps that you think you already understand. This process is more about configuration than convention.
3. This example is specifically for nopCommerce v1.8. I believe it will work for 1.7 as well. < 1.7 uses the ADO.Net convention. For 1.9 the process is similar but instead of manager classes you will need to implement interfaces instead.
4. Tables that have relationships adds another wrinkle to this process. It is not difficult but at this time, outside the scope of this tutorial.
5. Keep in mind that this tutorial is NOT upgrade ready. Because we directly modified NopModel.edmx and NopObjectContext.cs, any future upgrade will over write what you have done. If you have more than one table you are adding or you just want to future proof (although this is not really a concern after 1.9) your application, I would recommend creating a separate data model project for your custom application. This is not that difficult to achieve.
I think that is it. I hope this is helpful. Let me know if anyone finds any errors.
trevor