Base solution for your next web application
Open Closed

Eager loading in repository #789


User avatar
0
noah created

I have an Entity Framework entity model that has a virtual navigation property for lazy loading. When using an Include extension method on the GetAll() from the template's Application project, it seems to work to eager load the property. However, I added an additional project to the solution and when calling the same method through an IAppService-based class , this error is thrown:

"The operation cannot be completed because the DbContext has been disposed."

Here is the method in my custom repository class:

public List<ActionDefinition> GetAllWithSchedule()
        {
            var query = GetAll();
            var actions = query.Include(action => action.Schedule).ToList();
            return actions;
        }

What do I need to do to handle repository access from a different, new project not included in the template?


5 Answer(s)
  • User Avatar
    0
    noah created

    The question here actually might be how to handle dependency injection for a project that is not based on the ABP template. I've added a standard VS web project alongside the ABP projects in my solution. I've attempted to provide the IOC behavior this way:

    protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
                GlobalConfiguration.Configure(WebApiConfig.Register);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
    
                AbpBootstrapper bootstrapper = new AbpBootstrapper();
                bootstrapper.Initialize();
                bootstrapper.IocManager.RegisterIfNot<IAssemblyFinder, CurrentDomainAssemblyFinder>();
            }
    

    Is there a better way to ensure the functionality in other projects is resolved correctly especially for Entity Framework use?

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    Is your project another standalone web project that shares same core and application layers? Or it's a class library project used by the main Web project. Projects should include module definition classes and declare dependencies. For web projects, it's easier to just copy current web project, delete all the code and change the running port.

  • User Avatar
    0
    noah created

    Thanks. This is a web project to run as a separate web app but access the main database -- architecture is two websites, one as web project from template, and another running Hangfire as a scheduler. I can try copying the web project, although this seems like it could carry a lot of overhead just to get a simple web project working that doesn't require all the other infrastructure.

    A simpler question: what needs to be done to ensure an entity can be eager loaded from a new project?

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    A simpler question: what needs to be done to ensure an entity can be eager loaded from a new project?

    Eager loading is not related to being a new project or existing project. It's a feature of EntityFramework eventually.

    I think your new web project is not properly configured. Eager loading is just a side effect of that.

    There are some steps to integrate your web application to the solution.

    • Your MvcApplication in global.asax should inherit from AbpWebApplication (example: <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero/blob/master/src/MyCompanyName.AbpZeroTemplate.Web/Global.asax.cs#L9">https://github.com/aspnetzero/aspnet-ze ... asax.cs#L9</a>)

    • You should include a module definition file (example: <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero/blob/master/src/MyCompanyName.AbpZeroTemplate.Web/App_Start/AbpZeroTemplateWebModule.cs#L36">https://github.com/aspnetzero/aspnet-ze ... ule.cs#L36</a>) and add needed DependsOn attribute(s) as in this class.

    These should be done in minimum.

  • User Avatar
    0
    noah created

    Thanks. I'm going to try the route of copy/rename the Web project. It took a couple hours to fix up naming conflicts etc, but seems to run now.