Development Plugin Multitable

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

I'm developing a plugin to add special discount functionality in nopcommerce. My plugin uses three custom made tables. After installing the plugin works fine, but after a while it throws an exception "The entity type GiftRequired is not part of the model for the current context". My plugin works through an actionfilter that calls a service of my plugin. During this call the exception is thrown.

Here is an example of my code, the three tables have the same structure (GiftRecord, GiftRequired, GiftReward). I did not include all my code just the parts that are usefull.

Anyone has an idea why my plugin suddenly stops working?


##GiftMap

public class GiftMap : EntityTypeConfiguration<GiftRecord>
{
        public GiftMap()
        {
            ToTable("DebuggedDiscount");

            //Map the primary key
            HasKey(m => m.Id);
            //Map the additional properties
            Property(m => m.Name).IsOptional();
            Property(m => m.Start).IsOptional();
            Property(m => m.End).IsOptional();
            Property(m => m.Maximum).IsOptional();
        }
}


##GiftRecord

public class GiftRecord : BaseEntity
{
        public virtual string Name { get; set; }
        public virtual DateTime? Start { get; set; }
        public virtual DateTime? End { get; set; }
        public virtual int Maximum { get; set; }  
}


##DependencyRegistar

public class DependencyRegistrar : IDependencyRegistrar
{
        private const string CONTEXT_NAME_GIFT = "nop_object_context_gift";

        public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config)
        {
            builder.RegisterType<GiftService>().As<IGifts>().InstancePerLifetimeScope();
            builder.RegisterType<DiscountActionFilter>().As<IFilterProvider>();
            builder.RegisterType<CheckOutDiscountActionFilter>().As<IFilterProvider>();

            //data context
            this.RegisterPluginDataContext<GiftObjectContext>(builder, CONTEXT_NAME_GIFT);
          
            //override required repository with our custom context
            builder.RegisterType<EfRepository<GiftRecord>>()
                .As<IRepository<GiftRecord>>()
                .WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT_NAME_GIFT))
                .InstancePerLifetimeScope();

            ////override required repository with our custom context
            builder.RegisterType<EfRepository<GiftReward>>()
                .As<IRepository<GiftReward>>()
                .WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT_NAME_GIFT))
                .InstancePerLifetimeScope();

            ////override required repository with our custom context
            builder.RegisterType<EfRepository<GiftRequired>>()
                .As<IRepository<GiftRequired>>()
                .WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT_NAME_GIFT))
                .InstancePerLifetimeScope();
        }

        public int Order
        {
            get { return 1; }
        }
}


##GiftService

public class GiftService : IGifts
{
        private readonly IRepository<GiftRecord> _giftRecordRepository;
        private readonly IRepository<GiftReward> _giftRewardRepository;
        private readonly IRepository<GiftRequired> _giftRequiredRepository;

        public GiftService(IRepository<GiftRecord> giftRecordRepository, IRepository<GiftReward> giftRewardRepository, IRepository<GiftRequired> giftRequiredRepository, IRepository<Product> productRepository)
        {
            _giftRecordRepository = giftRecordRepository;
            _giftRewardRepository = giftRewardRepository;
            _giftRequiredRepository = giftRequiredRepository;
        }

        public IList<GiftRecord> GetAllDiscounts(string couponCode = "", string discountName = "", bool showHidden = false)
        {
            return _giftRecordRepository.Table.ToList();
        }
}


##ActionFilter

public class DiscountActionFilter : ActionFilterAttribute, IFilterProvider
{
        
        public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
        {
            if (actionDescriptor.ActionName.Contains("AddProductToCart"))
            {
                return new List<Filter>() { new Filter(this, FilterScope.Action, 0) };
            }

            return new List<Filter>();

        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //Required services
            var _productService = EngineContext.Current.Resolve<IProductService>();
            var _workContext = EngineContext.Current.Resolve<IWorkContext>();
            var _storeContext = EngineContext.Current.Resolve<IStoreContext>();
            var _giftService = EngineContext.Current.Resolve<IGifts>();
            var _shoppingCartService = EngineContext.Current.Resolve<IShoppingCartService>();
            var _productAttributeParser = EngineContext.Current.Resolve<IProductAttributeParser>();
            var _productAttributeService = EngineContext.Current.Resolve<IProductAttributeService>();


            //Get all active discounts (exception is thrown at this part)
            IList<GiftRequired> requirements = _giftService.GetAllRequired();

            //Get shopping cart of current customer
            var cart = _workContext.CurrentCustomer.ShoppingCartItems
                .Where(sci => sci.ShoppingCartType == (ShoppingCartType)1)
                .LimitPerStore(_storeContext.CurrentStore.Id)
                .ToList();

        }
}      
6 years ago
As suggested in the error message, where is your "GiftRequired" entity? :)
6 years ago
Hello,

GiftRequired is build the same way as the other entities of my plugin. Sometimes i get the exact same error but with a different entity (GiftRequired or GiftReward).


public class GiftRequired : BaseEntity
{
     public virtual int Gift_Id { get; set; }
     public virtual int Product_Id { get; set; }
     public virtual int Amount { get; set; }
}



public class GiftRequiredMap : EntityTypeConfiguration<GiftRequired>
{
     public GiftRequiredMap()
     {
         ToTable("DebuggedGiftRequired");

         //Map the primary key
         HasKey(m => m.Id);
         //Map the additional properties
         Property(m => m.Gift_Id);
         Property(m => m.Product_Id);
         Property(m => m.Amount);
    }
}
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.