Example #1
0
        static IServiceCollection AddFreeDbContext(this IServiceCollection services, Type dbContextType, Action <DbContextOptionsBuilder> options)
        {
            services.AddScoped(dbContextType, sp =>
            {
                DbContext ctx = null;
                try
                {
                    var ctor       = dbContextType.GetConstructors().FirstOrDefault();
                    var ctorParams = ctor.GetParameters().Select(a => sp.GetService(a.ParameterType)).ToArray();
                    ctx            = Activator.CreateInstance(dbContextType, ctorParams) as DbContext;
                }
                catch (Exception ex)
                {
                    throw new Exception(DbContextStrings.AddFreeDbContextError_CheckConstruction(dbContextType.Name), ex);
                }
                if (ctx != null && ctx._ormScoped == null)
                {
                    var builder = new DbContextOptionsBuilder();
                    options(builder);
                    ctx._ormScoped   = DbContextScopedFreeSql.Create(builder._fsql, () => ctx, () => ctx.UnitOfWork);
                    ctx._optionsPriv = builder._options;

                    if (ctx._ormScoped == null)
                    {
                        throw new Exception(DbContextStrings.ConfigureUseFreeSql);
                    }

                    ctx.InitPropSets();
                }
                return(ctx);
            });
            return(services);
        }
Example #2
0
        public void AddFreeDbContextError_CheckConstructionTest()
        {
            string text = DbContextStrings.AddFreeDbContextError_CheckConstruction("1");

            output.WriteLine(text);
        }