示例#1
0
        public void TestExampleSetupViaOnConfiguringOk()
        {
            //SETUP
            const string connectionString                                     //#A
                = "Server=(localdb)\\mssqllocaldb;Database=EfCore.TestSupport-Test;Trusted_Connection=True";
            var builder = new                                                 //#B
                          DbContextOptionsBuilder <DbContextOnConfiguring>(); //#B

            builder.UseSqlServer(connectionString);                           //#C
            var options = builder.Options;                                    //#D

            //ATTEMPT
            using (var context = new DbContextOnConfiguring //#B
                                     (options))             //#B
            {
                //VERIFY
                context.Database.GetDbConnection().ConnectionString.ShouldEqual(connectionString);
            }

            /********************************************************************
            #A This holds the connection string for the database to be used for the unit test
            #B I set up the options I want to use
            #C I then provide the options to the DbContext via is new, one-parameter constructor
            * ******************************************************************/
        }
示例#2
0
        public void TestExampleSetupViaConstructorOk()
        {
            //SETUP
            const string connectionString                          //#A
                = "Server=(localdb)\\mssqllocaldb;Database=EfCore.TestSupport-Test;Trusted_Connection=True";
            var builder = new                                      //#B
                          DbContextOptionsBuilder <BookContext>(); //#B

            builder.UseSqlServer(connectionString);                //#C
            var options = builder.Options;                         //#D

            using (var context = new BookContext(options))         //#E
            {
                //VERIFY
                context.Database.GetDbConnection().ConnectionString.ShouldEqual(connectionString);
            }

            /********************************************************************
            #A This holds the connection string for the SQL Server database
            #B We need to create DbContextOptionsBuilder<T> class to build the options
            #C Here I define that I want to use the SQL Server database provider
            #D I then build the final DbContextOptions<EfCoreContext> options that the application's DbContext needs
            #E This then allows me to create an instance for my unit tests
            * ******************************************************************/
        }