Exemplo n.º 1
0
        /// <summary>
        /// 获取 DbContext 构造函数
        /// </summary>
        /// <param name="dbContextProvider"></param>
        /// <param name="dbContextConfiguration"></param>
        /// <returns></returns>
        private ConstructorInfo GetDbContextConstructor(IDbContextProvider dbContextProvider, DbContextConfiguration dbContextConfiguration)
        {
            if (_dbContextConstructorDict.TryGetValue(dbContextProvider.DbContextType, out ConstructorInfo constructor))
            {
                return(constructor);
            }

            var constructors = dbContextProvider.DbContextType.GetConstructors();

            constructor = constructors.Where(o =>
            {
                var parameterInfos = o.GetParameters();

                var dbContextOptionsParameter = parameterInfos
                                                .FirstOrDefault(parameter => parameter.ParameterType == dbContextConfiguration.DbContextOptions.Options.GetType().BaseType);

                return(dbContextOptionsParameter != null);
            }).FirstOrDefault();

            if (constructor == null)
            {
                throw new ArgumentException($"The DbContextType {dbContextProvider.DbContextType.FullName} does not have a constructor with a DbContextOptions argument");
            }

            _dbContextConstructorDict[dbContextProvider.DbContextType] = constructor;
            return(constructor);
        }