Exemple #1
0
        private static async Task SetupAsync(EmulatorRunner emulatorRunner)
        {
            Console.WriteLine("");
            Console.WriteLine("Starting emulator...");
            emulatorRunner.StartEmulator().WaitWithUnwrappedExceptions();
            Console.WriteLine("");

            DatabaseName databaseName = DatabaseName.FromProjectInstanceDatabase(s_projectId, s_instanceId, s_databaseId);

            await MaybeCreateInstanceOnEmulatorAsync(databaseName.ProjectId, databaseName.InstanceId);
            await MaybeCreateDatabaseOnEmulatorAsync(databaseName);
        }
Exemple #2
0
        internal static async Task RunSampleApp()
        {
            // Set the SPANNER_EMULATOR_HOST environment variable for this process. This
            // ensures that the Entity Framework provider will connect to the emulator
            // instead of to the real Google Cloud Spanner. Remove this line if you want
            // to test the application against a real Spanner database.
            Environment.SetEnvironmentVariable("SPANNER_EMULATOR_HOST", "localhost:9010");
            var emulatorRunner = new EmulatorRunner();

            try
            {
                // This starts an in-mem emulator and creates the sample database.
                await SetupAsync(emulatorRunner);

                // Create the connection string that will be used.
                DatabaseName databaseName     = DatabaseName.FromProjectInstanceDatabase(s_projectId, s_instanceId, s_databaseId);
                var          dataSource       = $"Data Source={databaseName}";
                var          connectionString = new SpannerConnectionStringBuilder(dataSource)
                {
                    EmulatorDetection = EmulatorDetection.EmulatorOrProduction,
                }.ConnectionString;
                Console.WriteLine($"Connecting to database {connectionString}");

                // Create a DbContext that uses our sample Spanner database.
                using var context = new SpannerTutorialContext(connectionString);
                var singer = new Singer
                {
                    SingerId  = Guid.NewGuid(),
                    FirstName = "Bob",
                    LastName  = "Allison",
                };
                context.Singers.Add(singer);
                var album = new Album
                {
                    AlbumId = Guid.NewGuid(),
                    Title   = "Let's Go",
                    Singer  = singer,
                };
                context.Albums.Add(album);
                var track = new Track
                {
                    Album   = album,
                    TrackId = 1L,
                    Title   = "Go, Go, Go",
                };
                context.Tracks.Add(track);

                // This saves all the above changes in one transaction.
                Console.WriteLine($"Writing Singer, Album and Track to the database");
                var count = await context.SaveChangesAsync();

                Console.WriteLine($"{count} records written to the database\n");

                // Get a single entity. Note that the primary key of Track consists
                // of both the parent AlbumId and the Id of the Track.
                var foundTrack = await context.Tracks.FindAsync(album.AlbumId, 1L);

                Console.WriteLine($"Found track {track.Title}");
                // You can use LINQ to query the data that was written.
                var singers = await context.Singers
                              .Where(s => s.FullName == "Bob Allison")
                              .ToListAsync();

                Console.WriteLine($"Found {singers.Count} singer(s) with full name {singers.First().LastName}");
            }
            finally
            {
                Console.WriteLine("");
                Console.WriteLine("Stopping emulator...");
                emulatorRunner.StopEmulator().WaitWithUnwrappedExceptions();
                Console.WriteLine("");
            }
        }