Search code examples
entity-framework.net-corecastle-windsorcastle

How to register Ef core db context with Castle windsor?


I " ve got a project under .net core. I want to register Ef Core Context with Castle windosr But I couldn 't find a solution to EfCore Wireup context in .net core. Thank you.


Solution

  • If you want to do this , first you need to know that you have a Context that has a DbContextOptionsBuilder parameter that has a DbContextOptionsBuilder parameter if you have added these constructor , you need to register this too , and now the code I " ve written below makes you less self - sufficient to use the OnConfiguring method.

       public static class DbContextFactoryBuilder
    {
        public static IDbContext Create(string connectionString)
        {
            var result = new MyDbContext(new DbContextOptionsBuilder().UseSqlServer(connectionString).Options);
            return result;
        }
    }
    

    and code for register in castle.

    container.Register(Component.For<MyDbContext>().UsingFactoryMethod(c => DbContextFactoryBuilder.Create(@"---your connection string---")));