Пример #1
0
        /// <summary>
        /// 添加数据库连接字符串
        /// </summary>
        /// <param name="services"></param>
        /// <param name="name">连接字符串名称</param>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <returns></returns>
        public static IServiceProvider AddConnectionString([NotNull] this IServiceProvider serviceProvider, [NotNull] string name, [NotNull] string connectionString)
        {
            Check.NotNull(serviceProvider, nameof(serviceProvider));
            Check.NotNullOrEmpty(name, nameof(name));
            Check.NotNullOrEmpty(connectionString, nameof(connectionString));

            var connectionStringStore = serviceProvider.GetService <IConnectionStringStore>();

            var connectionStringProvider = new ConnectionStringProvider(name, connectionString);

            connectionStringStore.CreateOrUpdate(connectionStringProvider);


            return(serviceProvider);
        }
Пример #2
0
        /// <summary>
        /// 添加数据库连接字符串
        /// </summary>
        /// <param name="services"></param>
        /// <param name="name">连接字符串名称</param>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <returns></returns>
        public static IServiceCollection AddConnectionString([NotNull] this IServiceCollection services, [NotNull] string name, [NotNull] string connectionString)
        {
            Check.NotNull(services, nameof(services));
            Check.NotNullOrEmpty(name, nameof(name));
            Check.NotNullOrEmpty(connectionString, nameof(connectionString));

            var count = services.Where(o => o.ImplementationInstance is IConnectionStringProvider)
                        .Select(o => (o.ImplementationInstance as IConnectionStringProvider))
                        .Count(o => o.Name == name);

            if (count > 0)
            {
                throw new ArgumentException($"A connection string with the name {name} already exists");
            }

            var connectionStringProvider = new ConnectionStringProvider(name, connectionString);

            services.AddSingleton <IConnectionStringProvider>(connectionStringProvider);

            return(services);
        }