Problem while adding an entity in nopCommerce 1.9

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 years ago
Thnx rcnopcom. It may be possible to add a new item to an existing entity. but it seems difficult to me to add a completely new entity ...
13 years ago
As a test I followed your steps and with the link below. You seem to have way more steps.


I followed the steps in http://blogs.planetcloud.co.uk/mygreatdiscovery/post/How-to-extend-nopCommerce.aspx
From the part: "Adding a new entity"

I always add the name MOD_ (MODification) to my variables or tables: MOD_License

> Table creation in database:

/****** Object:  Table [dbo].[MOD_License] ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[MOD_License](
  [LicenseID] [int] IDENTITY(1,1) NOT NULL,
  [Email] [nvarchar](100) NOT NULL CONSTRAINT [DF_MOD_License_Email]  DEFAULT (''),
  [WebshopData] [nvarchar](max) NOT NULL,
  [ProductVariantID] [int] CONSTRAINT [DF_MOD_License_ProductVariantID]  DEFAULT ((0)),
  [SKU] [nvarchar](100)   CONSTRAINT [DF_MOD_License_SKU]  DEFAULT (''),
  [Type] [int] CONSTRAINT [DF_MOD_License_Type]  DEFAULT ((0)),
  [LicenseBasedOn] [nvarchar](100)  CONSTRAINT [DF_MOD_License_LicenseBasedOn]  DEFAULT (''),
  [ReleasedLicenseNo] [nvarchar](100) CONSTRAINT [DF_MOD_License_ReleasedLicenseNo]  DEFAULT (''),
CONSTRAINT [PK_MOD_License] PRIMARY KEY CLUSTERED
(
  [LicenseID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO


> After that I created the following class under Nop.BusinessLogic in the folder named MOD
MOD_License.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using NopSolutions.NopCommerce.BusinessLogic.MOD;
using NopSolutions.NopCommerce.BusinessLogic.Data;

namespace NopSolutions.NopCommerce.BusinessLogic.MOD
{
    public partial class MOD_License
    {
        public int LicenseID { get; set; }
        public string Email { get; set; }
        public string WebshopData { get; set; }
        public int ProductVariantID { get; set; }
        public string SKU { get; set; }
        public int Type { get; set; }
        public string LicenseBasedOn { get; set; }
        public string ReleasedLicenseNo { get; set; }
    }
}


> Add entity to NopModel.edmx

> Added the following to the existing MOD_License.cs

namespace NopSolutions.NopCommerce.BusinessLogic.Data
{
    /// <summary>
    /// Represents a nopCommerce object context
    /// </summary>
    public partial class NopObjectContext : ObjectContext
    {
        private ObjectSet<MOD_License> _MOD_Licenses;

        public ObjectSet<MOD_License> MOD_Licenses
        {
            get
            {
                if ((_MOD_Licenses == null))
                    _MOD_Licenses = CreateObjectSet<MOD_License>();
                return _MOD_Licenses;
            }
        }
    }
}

namespace NopSolutions.NopCommerce.BusinessLogic.MOD
{
    public class MOD_LicenseManager
    {
        public static MOD_License GetMOD_LicenseById(int LicenseId)
        {
            var db = ObjectContextHelper.CurrentObjectContext;
            return db.MOD_Licenses.SingleOrDefault(x => x.LicenseID == LicenseId);
        }

        public static MOD_License GetMOD_LicenseByEmail(string sEmail)
        {
            var db = ObjectContextHelper.CurrentObjectContext;
            return db.MOD_Licenses.SingleOrDefault(x => x.Email.ToLower() == sEmail.ToLower());
        }

        public static IList<MOD_License> GetAllLicenses()
        {
            var db = ObjectContextHelper.CurrentObjectContext;
            return db.MOD_Licenses.OrderBy(x => x.Email).ToList();
        }

        public static void Save(MOD_License license)
        {
            var db = ObjectContextHelper.CurrentObjectContext;

            if (license.LicenseID == 0)
            {
                // new record
                db.MOD_Licenses.AddObject(license);
            }
            else
            {
                // existing record
                if (!db.IsAttached(license))
                    db.MOD_Licenses.Attach(license);
            }

            db.SaveChanges();
        }
    }
}


> Finally I created , build and run Licenses.aspx page:

13 years ago
In case it helps, in 1.90 you no longer need to manually add the ObjectSet for your new entity to the object context. This is generated using tt templates as soon as you save the edmx.

However, if you are adding a new entity in a NEW namespace, you will need to update the tt template used for generating the object context to include your namespace (just add a using statement at the top).

The other big change is that we 1.90 uses dependency injection. If you look at how some of the other services are constructed you should figure it out.

HTH
13 years ago
I think you need to register your service AlbumService in case you haven't done it.
In NopSolutions.NopCommerce.BusinessLogic.Infrastructure.UnityDependencyResolver there is a method ConfigureContainer that registers all interfaces with their types in the UnityConatainer, so that you can resolve them with Resolve<IAlbumService>. Just add this code in this method:
container.RegisterType<IAlbumService, AlbumService>(new UnityPerExecutionContextLifetimeManager());

Hope this helps!
13 years ago
True 7Spikes, that's how i figured out the same problem. But i find this is a dirty way.

We can see in UnityDependencyResolver.cs the following comment :

//Take into account that Types and Mappings registration could be also done using the UNITY XML configuration
//But we prefer doing it here (C# code) because we'll catch errors at compiling time instead execution time, if any type has been written wrong.

Somebody has an idea about the UNITY XML configuration and how to implement this ?
13 years ago
As well as registering in the UnityDependencyResolver I had also to add to the BaseNopMasterPage.cs and the BaseNopUserControl.cs.  Is there an easier way?
(And I am still getting problems at run time which I need to resolve.)
12 years ago
Hi,

I might be very late for the reply, but I was having exactly the same problem. Nop has very little help

on the Internet. After struggling for 2 to 3 hours I had found the solution. Here is the Solution.

You need to add

container.RegisterType<IAlbumService, AlbumService>(new UnityPerExecutionContextLifetimeManager());

to UnityDependencyResolver.cs file in the name space NopSolutions.NopCommerce.BusinessLogic.Infrastructure


Hope someone will find this helpful.
12 years ago
You have to Set in the Register Manager.

Check the class UnityDependencyResolver in BussinesLogic.Infrastructure

in this class has the Lifetime.

Check it!
11 years ago
where does ObjectContextHelper come from?
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.