/// <summary>
        /// Created a Sqlite Options for in-memory database.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="throwOnClientServerWarning">Optional: default will throw exception if QueryClientEvaluationWarning is logged. Set to false if not needed</param>
        /// <returns></returns>
        public static DbContextOptions <T> CreateOptions <T>
            (bool throwOnClientServerWarning = true) //#A
            where T : DbContext
        {
            //Thanks to https://www.scottbrady91.com/Entity-Framework/Entity-Framework-Core-In-Memory-Testing
            var connectionStringBuilder =                                   //#B
                                          new SqliteConnectionStringBuilder //#B
            {
                DataSource = ":memory:"
            };                                                         //#B
            var connectionString =                                     //#C
                                   connectionStringBuilder.ToString(); //#C
            var connection =                                           //#D
                             new SqliteConnection(connectionString);   //#D

            connection.Open();                                         //#E             //see https://github.com/aspnet/EntityFramework/issues/6968

            // create in-memory context
            var builder =
                new DbContextOptionsBuilder <T>();

            builder.UseSqlite(connection);    //#F
            builder.ApplyOtherOptionSettings  //#G
                (throwOnClientServerWarning); //#G

            return(builder.Options);          //#H
        }
Esempio n. 2
0
        //------------------------------------
        //private methods

#if NETSTANDARD2_0
        private static DbContextOptionsBuilder <T> CreateOptionWithDatabaseName <T>(object callingClass, bool throwOnClientServerWarning,
                                                                                    string callingMember = null)
            where T : DbContext
        {
            var connectionString = callingClass.GetUniqueDatabaseConnectionString(callingMember);
            var builder          = new DbContextOptionsBuilder <T>();

            builder.UseSqlServer(connectionString);
            builder.ApplyOtherOptionSettings(throwOnClientServerWarning);

            return(builder);
        }
Esempio n. 3
0
        private static DbContextOptionsBuilder <T> CreateOptionWithDatabaseName <T>(object callingClass,
                                                                                    string callingMember, Action <DbContextOptionsBuilder <T> > applyExtraOption)
            where T : DbContext
        {
            var connectionString = callingClass.GetUniqueDatabaseConnectionString(callingMember);
            var builder          = new DbContextOptionsBuilder <T>();

            builder.UseSqlServer(connectionString);
            builder.ApplyOtherOptionSettings();
            applyExtraOption?.Invoke(builder);

            return(builder);
        }
Esempio n. 4
0
        //------------------------------------------
        //private methods

        private static DbContextOptionsBuilder <T> CreateOptionWithDatabaseName <T>(this string databaseName, Action <DbContextOptionsBuilder <T> > builder)
            where T : DbContext
        {
            var localBuilder = new DbContextOptionsBuilder <T>()
                               .UseCosmos(
                "https://localhost:8081",
                "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
                databaseName);

            localBuilder.ApplyOtherOptionSettings();
            builder?.Invoke(localBuilder);
            return(localBuilder);
        }
Esempio n. 5
0
        /// <summary>
        /// This creates the options for an in-memory database, with the name given.
        /// </summary>
        /// <typeparam name="TContext"></typeparam>
        /// <param name="dbName">name of in-memory database</param>
        /// <param name="throwOnClientServerWarning">Optional: default will throw exception if QueryClientEvaluationWarning is logged. Set to false if not needed</param>
        /// <returns></returns>
        public static DbContextOptions <TContext> CreateOptions <TContext>
            (this string dbName, bool throwOnClientServerWarning = true)
            where TContext : DbContext
        {
            // Create a fresh service provider, and therefore a fresh
            // InMemory database instance.
            var serviceProvider = new ServiceCollection()
                                  .AddEntityFrameworkInMemoryDatabase()
                                  .BuildServiceProvider();

            // Create a new options instance telling the context to use an
            // InMemory database and the new service provider.
            var builder = new DbContextOptionsBuilder <TContext>();

            builder.UseInMemoryDatabase(dbName)
            .UseInternalServiceProvider(serviceProvider);
#if NETSTANDARD2_0
            builder.ApplyOtherOptionSettings(throwOnClientServerWarning);
#elif NETSTANDARD2_1
            builder.ApplyOtherOptionSettings();
#endif
            return(builder.Options);
        }
        CreateOptionWithDatabaseName <T>(     //#E
            object callingClass,              //#F
            bool throwOnClientServerWarning,  //#F
            string callingMember = null)      //#F
            where T : DbContext
        {
            var connectionString = callingClass                        //#G
                                   .GetUniqueDatabaseConnectionString( //#G
                callingMember);                                        //#G
            var builder =                                              //#H
                          new DbContextOptionsBuilder <T>();           //#H

            builder.UseSqlServer(connectionString);                    //#H
            builder.ApplyOtherOptionSettings(                          //#I
                throwOnClientServerWarning);                           //#I

            return(builder.Options);                                   //#J
        }
Esempio n. 7
0
        /// <summary>
        /// Created a Sqlite Options for in-memory database.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        private static DbContextOptionsBuilder <T> SetupConnectionAndBuilderOptions <T>() //#A
            where T : DbContext
        {
            //Thanks to https://www.scottbrady91.com/Entity-Framework/Entity-Framework-Core-In-Memory-Testing
            var connectionStringBuilder =                                   //#B
                                          new SqliteConnectionStringBuilder //#B
            {
                DataSource = ":memory:"
            };                                                             //#B
            var connectionString = connectionStringBuilder.ToString();     //#C
            var connection       = new SqliteConnection(connectionString); //#D

            connection.Open();                                             //#E             //see https://github.com/aspnet/EntityFramework/issues/6968

            // create in-memory context
            var builder = new DbContextOptionsBuilder <T>();

            builder.UseSqlite(connection);      //#F
            builder.ApplyOtherOptionSettings(); //#G

            return(builder);                    //#H
        }
Esempio n. 8
0
        SetupConnectionAndBuilderOptions <T>                         //#D
            (Action <DbContextOptionsBuilder <T> > applyExtraOption) //#E
            where T : DbContext
        {
            //Thanks to https://www.scottbrady91.com/Entity-Framework/Entity-Framework-Core-In-Memory-Testing
            var connectionStringBuilder =                                   //#F
                                          new SqliteConnectionStringBuilder //#F
            {
                DataSource = ":memory:"
            };                                                             //#F
            var connectionString = connectionStringBuilder.ToString();     //#G
            var connection       = new SqliteConnection(connectionString); //#H

            connection.Open();                                             //#I             //see https://github.com/aspnet/EntityFramework/issues/6968

            // create in-memory context
            var builder = new DbContextOptionsBuilder <T>();

            builder.UseSqlite(connection);      //#J
            builder.ApplyOtherOptionSettings(); //#K
            applyExtraOption?.Invoke(builder);  //#L

            return(builder);                    //#M
        }