Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • kapil 21 posts 114 karma points
    Apr 30, 2019 @ 18:01

    Register dbContext through composition

    I want to use the Microsoft.EntityFrameworkCore.dbContext into the controller.

    I would really appreciate if I can get to know how we can register dependency for that in:

    public class Global : UmbracoApplication, IComposer
            public void Compose(Composition composition)
                composition.Register<IRepository1, Repository1>();
                // want to register dbContext here with so that I can use it like mentioned in section 2


      public class Repository1 : IRepository1
            private SomeDbContext _context;
            public Repository1(SomeDbContext dbContext)
                _context = dbContext;
  • Corné Strijkert 80 posts 453 karma points c-trib
    May 01, 2019 @ 10:12
    Corné Strijkert

    Hi kapil,

    That should not be that difficult. Give it a try with:



    composition.Register<SomeDbContext>(factory => new SomeDbContext(), Lifetime.Scope);

    in your component.

    Instead of implementing the IComposer interface you should implement the IUserComposer interface.

    It is good to know that Umbraco 8 uses LightInject for DI, so you can search the www for specific scenarios with LightInject.

    You can also take a look at to see different ways you can register stuff.

  • kapil 21 posts 114 karma points
    May 01, 2019 @ 20:34

    Thanks for your reply Corne. factory option worked with the combination of passing new dbContextOption builder

Please Sign in or register to post replies

Write your reply to: