/// <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
        }
Beispiel #2
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
        }