コード例 #1
0
 public static DbContextOptionsBuilder UseSql(this DbContextOptionsBuilder builder,
                                              DbServerTypes serverType,
                                              string connectionString)
 {
     return(serverType == DbServerTypes.Sqlite
         ? builder.UseSqlite(connectionString)
         : builder.UseSqlServer(connectionString));
 }
コード例 #2
0
 public ChessGamesDbContext(
     DbServerTypes serverType,
     string connectionString,
     ILoggerFactory loggerFactory = null)
     : base(new DbContextOptionsBuilder()
            .UseSql(serverType, connectionString)
            .Options)
 {
     _loggerFactory = loggerFactory;
 }
コード例 #3
0
ファイル: DbStartup.cs プロジェクト: Chrislee187/chess.db
        /// <summary>
        /// Add a SQL based ChessGamesDbContext to the container
        /// </summary>
        /// <param name="services"></param>
        /// <param name="serverType"></param>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        public static IServiceCollection AddChessDatabaseContext(
            this IServiceCollection services,
            DbServerTypes serverType,
            string connectionString)
        {
            return(services.AddDbContext <ChessGamesDbContext>(
                       opts =>
            {
                switch (serverType)
                {
                case DbServerTypes.SqlServer:
                    opts.UseSqlServer(connectionString);
                    break;

                case DbServerTypes.Sqlite:
                    opts.UseSqlite(connectionString);
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(serverType), serverType, null);
                }
            }));
        }