Error-"The specified LINQ expression contains references to queries that are associated with different contexts."

StackOverflow https://stackoverflow.com/questions/23222963

Question

I am using NopCommerce and am making a plugin using this tutorial: Plugin with data access

But when I run the application, I receive the following error:

"The specified LINQ expression contains references to queries that are associated with different contexts".

DependencyRegistrar Class :

 public class LookDependencyRegistrar : IDependencyRegistrar
        {
            private const string CONTEXT_NAME = "nop_object_context_look";

            public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder)
            {
                //data layer
                var dataSettingsManager = new DataSettingsManager();
                var dataProviderSettings = dataSettingsManager.LoadSettings();

                if (dataProviderSettings != null && dataProviderSettings.IsValid())
                {
                    //register named context
                    builder.Register<IDbContext>(c => new LookObjectContext(dataProviderSettings.DataConnectionString))
                        .Named<IDbContext>("nop_object_context_Look")
                        .InstancePerHttpRequest();

                    builder.Register<IDbContext>(c => new Look_Product_MappingObjectContext(dataProviderSettings.DataConnectionString))
                        .Named<IDbContext>("nop_object_context_Look_Product")
                        .InstancePerHttpRequest();

                    builder.Register<LookObjectContext>(c => new LookObjectContext(dataProviderSettings.DataConnectionString))
                        .InstancePerHttpRequest();
                    builder.Register<Look_Product_MappingObjectContext>(c => new Look_Product_MappingObjectContext(dataProviderSettings.DataConnectionString))
                       .InstancePerHttpRequest();

                }
                else
                {
                    //register named context
                    builder.Register<IDbContext>(c => new LookObjectContext(c.Resolve<DataSettings>().DataConnectionString))
                        .Named<IDbContext>("nop_object_context_Look")
                        .InstancePerHttpRequest();

                    builder.Register<IDbContext>(c => new Look_Product_MappingObjectContext(dataProviderSettings.DataConnectionString))
                        .Named<IDbContext>("nop_object_context_Look_Product")
                        .InstancePerHttpRequest();

                    builder.Register<LookObjectContext>(c => new LookObjectContext(c.Resolve<DataSettings>().DataConnectionString))
                        .InstancePerHttpRequest();
                    builder.Register<Look_Product_MappingObjectContext>(c => new Look_Product_MappingObjectContext(c.Resolve<DataSettings>().DataConnectionString))
                        .InstancePerHttpRequest();                    
                }

                //override required repository with our custom context
                builder.RegisterType<EfRepository<Look>>()
                    .As<IRepository<Look>>()
                    .WithParameter(ResolvedParameter.ForNamed<IDbContext>("nop_object_context_Look"))
                    .InstancePerHttpRequest();

                builder.RegisterType<EfRepository<Look_Product_Mapping>>()
                    .As<IRepository<Look_Product_Mapping>>()
                    .WithParameter(ResolvedParameter.ForNamed<IDbContext>("nop_object_context_Look_Product"))
                    .InstancePerHttpRequest();

                //Register services
                builder.RegisterType<LookService>().As<ILookService>();
            }
            public int Order
            {
                get { return 0; }
            }
        }

How can I solve this problem?

Was it helpful?

Solution

I think that's because your entities which you created in the plugin contains a relation between Nop.Core entity such as Product

Entity Framework does not support cross-project relations You have to remove the Nop.Core Entity such as (Product property) from your entity and use ProductId int instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top