Example #1
0
        public ApiBuilder AddDbContexts()
        {
            if (Options.ContextOption == null)
            {
                return(this);
            }
            var contexts = MapperUtils.GetAllContext();

            foreach (var context in contexts)
            {
                var builder = new DbContextOptionsBuilder();
                builder.UseLazyLoadingProxies();
                var lifetime        = context.GetCustomAttribute <ApiContext>()?.LifeTime ?? Enums.LifeTime.Transient;
                var serviceLifeTime = (ServiceLifetime)(int)lifetime;

                Services.Add(new ServiceDescriptor(context, p =>
                {
                    return(Activator.CreateInstance(context, Options.ContextOption(builder, p.GetService <IConfiguration>().GetConnectionString(context.Name)).Options));
                }, serviceLifeTime));
            }

            return(this);
        }