错误 - “指定的LINQ表达式包含对与不同上下文关联的查询的引用。”

时间:2014-04-22 14:51:02

标签: c# .net asp.net-mvc nopcommerce

我正在使用NopCommerce并使用本教程制作插件:Plugin with data access

但是当我运行应用程序时,我收到以下错误:

  

"指定的LINQ表达式包含对查询的引用   与不同的背景相关联"。

DependencyRegistrar类:

 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; }
            }
        }

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

我认为这是因为您在插件中创建的实体包含Nop.Core实体(如产品)之间的关系

实体框架不支持跨项目关系您必须从实体中删除Nop.Core实体,例如(Product property),而是使用ProductId int。