Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 840
John Avatar asked Aug 12 '18 06:08

John


1 Answers

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---")));
like image 55
amirhamini Avatar answered Nov 02 '22 23:11

amirhamini