For my lecture on ASP.NET Web API I needed some seed data for Entity Framework Core for the API with some relational data.
I was tired of creating this manually. Yes you have sites that can create dummy data for you but I really needed some relational data between my entities.
I needed following relationships between my entities:
One to many
Many to many
I thought it would be nice if I could do this with some data from Spotify. So I began to write my own Spotify scraper to create my seed data.
Created entities
With my tool I create several classes with their own properties. The entities are created as:
An artist can have many albums
An artist can have many genres
An album can have one artist
An album can have many tracks
An track can have one album
An genre can have many artists
Database scheme
Abstract base class
Properties
Id: the Id for the primary key in your database
Artist class
An Artist class that derives from the abstract EntityBase class.
Properties
Name: The name of the Artist in Spotify
Followers: the number of followers in Spotify
Popularity: the number of popularity in Spotify
ArtistGenres: a many-to-many class for the genres of the artist
Albums: a collections of the albums from the artist in Spotify
SpotifyId: the artist id in Spotify
Image: an Uri with the main image of the artist in Spotify
Album class
An Album class that derives from the abstract EntityBase class.
Properties
Name: The name of the Album of an artist in Spotify
ReleaseDate: the release date of the album know by Spotify
TotalTracks: the number of tracks of the album in Spotify
SpotifyId: the album id in Spotify
Image: an Uri with the main image of the album in Spotify
ArtistId: the foreign key to the artist of the album
Artist: a navigation property to the Artist of the album
Track class
An Track class that derives from the abstract EntityBase class.
Properties
Name: The name of the Track of an album in Spotify
SpotifyId: the track id in Spotify
TrackNumber: the track number of the album know by Spotify
DiscNumber: the disc number of the album know by Spotify
DurationMs: the duration of a song in milliseconds in Spotify
Explicit: an Boolean if the track is marked explicit or not in Spotify
PreviewUrl: an Uri to listen to a preview of the track
AlbumId: a primary key to the album that the track belongs to
Album: a navigation property to the Album of the track
Genre class
An Genre class that derives from the abstract EntityBase class.
Properties
Name: The name of the Genre in Spotify
ArtistGenres: the many to many navigation property to the Artists with this genre
ArtistGenre class
An ArtistGenre class.
Properties
ArtistId: The PK and FK of the ArtistId
GenreId: The PK and FK of the GenreId
Artist: The navigation property of the Artist
Genre: The navigation property of the Genre
Created seeding
The tool will create several files for your seeding.
DbContext class
An DbContext class with the name you have chosen in the tool. In this example is the name ApplicationDbContext.
Seeder classes
The tool will create five Seeder classes for the Artist, Genre, Track, Album and ArtistGenres seeding.
Let's take a look to some the files.
ArtistSeeder
AlbumSeeder
And of course this is the same for:
TrackSeeder
GenreSeeder
ArtistGenreSeeder
All the entities have a foreign key Guid ID that relate to a primary key Guid ID in the seeding.
You can find my Spotify seed data generator here. Below you'll find some sort of manual to use my tool.
Spotify seed data generator manual
3. Get your Spotify OAuth token
4. Use the Spotify seed data generator tool
6. Copy folders in your project
7. Configure your connectionstring
What you'll need
Spotify premium account (for your Spotify OAuth token)
ASP.NET Core application (API, MVC, Razor Pages, Blazor, ...)
Your favorite browser
Choose your artists
Go to Spotify Web player
Search for your favorite artist by clicking on "Search" or click here
Search "Metallica" for example:
Click on the name of the artist to go to the artist page
In the address bar of your browser you can now see the Spotify Artist ID of Metallica:
Copy this artist ID and paste it somewhere, we will need this ID later in the tool.
If you want additional artists, then repeat these steps to retain their Spotify artists ID's.
Get your Spotify OAuth token
Now it's time to get your (temporary) Spotify token. We will get this temporary token from the developer site from Spotify. If you already have a Spotify token, then you can skip this step and use your own token.
Go to this page to get some random Spotify artists through the Spotify Console for Developers. We won't need the artists but we only go to this page to get the Spotify token in a easy way.
Click on the green button "GET TOKEN":
Login with your Spotify Credentials
On the next screen you don't need the check any boxes for the scopes, you can click on the green button "REQUEST TOKEN":
You'll obtain an OAuth Token key in the textbox like this:
Copy this token and paste it somewhere, because we'll need this token in the tool to identity yourself to Spotify.
Keep in mind that this token expires really quick, so maybe you'll have to repeat this steps to get a new "fresh" token from Spotify.
Use the Spotify seed data generator tool
Go to the Spotify seed data generator tool
Fill in your Spotify OAuth token
Fill in your Spotify artist id's (If you have multiple ID's, then separate them by a comma)
Choose a name for your DbContext class
Choose a namespace for your DbContext class
Choose a namespace for your entity classes
Click on the GO button!
Wait :-)
Download the files
After the tool does the scraping throught the Spotify API you can download the zip file that contains all the nessecary files:
In the zip file you'll find two folders; Data and Entities:
The Entities folder contains all the entity classes and a folder that contains the BaseEntity class:
The Data folder contains the DbContext class and the Seeder classes:
Copy folders in your project
Create a new ASP.NET Core web project and copy the folders where you want them to be. For example:
Configure your connectionstring
Go to your appsettings.json and add
to your appsettings.json. So it would look like this. Of course you can change the database name or choose a different server.
Configure your Startup
Add the following code in the ConfigureServices method from your Startup.cs class:
If you've chosen a different name for your DbContext than you need the change ApplicationDbContext to the name you have chosen.
Install NuGet packages
Install the following NuGet packages:
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.Relational
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools (If you don't have them globally installed)
And import the required namespaces in your Startup.cs class.
Add migration
Open a PowerShell terminal at the root of your project and type in following command:
Update database
And to update your database type in following command:
Now open your database server manager of your choice, inspect the tables and you'll see that your tables contain data. In this example I opened the Albums table with the build-in Server Explorer in Visual Studio:
Feel free to (ab)use and/or share this tool. If you have any problems or suggestions, then ping me at Twitter, and I'll see what I can do for you.