NopCommerce - Insert Database | Adding a record to a table in the database with the plugin

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
5 years ago
Hi,

I'm developing a plugin.

I want to convert it into a model making Entity model.

The toEntity structure in MappingExtension does not work because of plugin. I know that.

How to change model entity in NopCommerce?

I want the entity model to return as a result of my model.

==============================

My goal is to add records to the database via plugin. It will be a communication form. I will record the values ​​entered in the database.
5 years ago
enesgezici wrote:
Hi,

I'm developing a plugin.

I want to convert it into a model making Entity model.

The toEntity structure in MappingExtension does not work because of plugin. I know that.

How to change model entity in NopCommerce?

I want the entity model to return as a result of my model.

==============================

My goal is to add records to the database via plugin. It will be a communication form. I will record the values ​​entered in the database.


Do you have an idea
5 years ago
Actually, you do not configure the Automapper in your plugin. You need to configure this at your plugin. The below code is working as expected at my nopcommerce 3.9 plugin. Hope it will help you. Please change the domain and ViewModel accordingly by yours.



  public class PluginMapperConfiguration : IMapperConfiguration
    {
        /// <summary>
        /// Get configuration
        /// </summary>
        /// <returns>Mapper configuration action</returns>
        public Action<IMapperConfigurationExpression> GetConfiguration()
        {
            //TODO remove 'CreatedOnUtc' ignore mappings because now presentation layer models have 'CreatedOn' property and core entities have 'CreatedOnUtc' property (distinct names)

            Action<IMapperConfigurationExpression> action = cfg =>
            {
                //products
  
                cfg.CreateMap<Product, ProductModelPlugin>()
                    .ForMember(dest => dest.CustomProperties, mo => mo.Ignore());               //ignore property

            
                cfg.CreateMap<ProductModelPlugin, Product>()
                    .ForMember(dest => dest.LimitedToStores, mo => mo.Ignore());     //ignore property
            };
            return action;
        }

        /// <summary>
        /// Order of this mapper implementation
        /// </summary>
        public int Order
        {
            get { return 0; }
        }
    }


Then



public static class MappingExtension
    {
        public static TDestination MapTo<TSource, TDestination>(this TSource source)
        {
            return AutoMapperConfiguration.Mapper.Map<TSource, TDestination>(source);
        }

        public static TDestination MapTo<TSource, TDestination>(this TSource source, TDestination destination)
        {
            return AutoMapperConfiguration.Mapper.Map(source, destination);
        }

        public static Product ToEntity(this ProductModelPlugin model, Product destination)
        {
            return model.MapTo(destination);
        }

        public static ProductModelPlugin ToModelPlugin(this Product entity)
        {
            return entity.MapTo<Product, ProductModelPlugin>();
        }

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




At controller

product = model.ToEntity(product);

5 years ago
sina.islam wrote:
Actually, you do not configure the Automapper in your plugin. You need to configure this at your plugin. The below code is working as expected at my nopcommerce 3.9 plugin. Hope it will help you. Please change the domain and ViewModel accordingly by yours.



  public class PluginMapperConfiguration : IMapperConfiguration
    {
        /// <summary>
        /// Get configuration
        /// </summary>
        /// <returns>Mapper configuration action</returns>
        public Action<IMapperConfigurationExpression> GetConfiguration()
        {
            //TODO remove 'CreatedOnUtc' ignore mappings because now presentation layer models have 'CreatedOn' property and core entities have 'CreatedOnUtc' property (distinct names)

            Action<IMapperConfigurationExpression> action = cfg =>
            {
                //products
  
                cfg.CreateMap<Product, ProductModelPlugin>()
                    .ForMember(dest => dest.CustomProperties, mo => mo.Ignore());               //ignore property

            
                cfg.CreateMap<ProductModelPlugin, Product>()
                    .ForMember(dest => dest.LimitedToStores, mo => mo.Ignore());     //ignore property
            };
            return action;
        }

        /// <summary>
        /// Order of this mapper implementation
        /// </summary>
        public int Order
        {
            get { return 0; }
        }
    }


Then



public static class MappingExtension
    {
        public static TDestination MapTo<TSource, TDestination>(this TSource source)
        {
            return AutoMapperConfiguration.Mapper.Map<TSource, TDestination>(source);
        }

        public static TDestination MapTo<TSource, TDestination>(this TSource source, TDestination destination)
        {
            return AutoMapperConfiguration.Mapper.Map(source, destination);
        }

        public static Product ToEntity(this ProductModelPlugin model, Product destination)
        {
            return model.MapTo(destination);
        }

        public static ProductModelPlugin ToModelPlugin(this Product entity)
        {
            return entity.MapTo<Product, ProductModelPlugin>();
        }

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




At controller

product = model.ToEntity(product);



Thank you Bro :) I solved it with your help. Thank you so much
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.