We need to update our Models to work with a more complex data schema.
See how far you can get using Copilot to perform the modifications to your existing classes. Copilot Chat would good to try if you have it installed.
Start by adding a new Artist
Model class that has these fields:
- ArtistId of type
int
- Name of type
string
Next, update the Album
Model so it has these fields:
- AlbumId of type
int
- GenreId of type
int
- ArtistId of type
int
- Title of type
string
- Price of type
decimal
- AlbumArtUrl of type
string
- Genre of type
Genre?
- Artist of type
Artist?
Finally, update the Genre
Model with these fields:
- GenreId of type
int
- Name of type
string
- Description of type
string
- Albumbs of type
List<Album>
Here we're going to deviate from the old MVC 3 tutorial as data seeding has changed a bit since then!
Create a new folder in the root of your project called Data
.
Add a new C# class file to the Data
folder called MusicStoreContext.cs
.
In the resulting file set the class to inherit from DbContext
.
Note
You will likely need to install these nuget packages to get database interactions working:
- Microsoft.EntityFrameworkCore
- Microsoft.EntityFrameworkCore.Relational
- Microsoft.EntityFrameworkCore.SqlServer
Your class should now look like this:
using Microsoft.EntityFrameworkCore;
using CopilotMvcMusicStore.Models;
namespace CopilotMvcMusicStore.Data
{
public class MusicStoreContext : DbContext
{
}
}
Go ahead and add three properties of type DbSet
for the Album
, Genre
and Artist
types.
Your class should now look like this.
using Microsoft.EntityFrameworkCore;
using CopilotMvcMusicStore.Models;
namespace CopilotMvcMusicStore.Data
{
public class MusicStoreContext : DbContext
{
public DbSet<Album> Albums { get; set; }
public DbSet<Genre> Genres { get; set; }
public DbSet<Artist> Artists { get; set; }
}
}
You will need to add a Constructor for this class. It should accept a DbContextOptions<MusicStoreContext>
argument and pass the inputs to the base constructor.
Add an overridden method named OnModelCreating
with the below signature and, in this method, add Entity mappings for our three types by using Copilot.
protected override void OnModelCreating(ModelBuilder modelBuilder) {}
You should be able to generate this entire method with Copilot prompts - maybe even just one!
We can use Entity Framework Code First to seed our database with test data. Generative AI services like GitHub Copilot are really good at repetitive, boilerplate code. Let's see how useful!
Leave this file open in a tab in Visual Studio.
Create a new C# class file in the Data
folder called DbInitializer.cs
.
Add the following using statements so you file looks like the below sample.
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using CopilotMvcMusicStore.Models;
namespace CopilotMvcMusicStore.Data
{
public class DbInitializer
{
}
}
Add the following method using a Copilot prompt. Use a multi-line comment to explain the requirement.
public static void Initialize(MusicStoreContext context) {}
Next let's do these steps:
- Inside this method ask Copilot to add a code to create an empty database schema if it doesn't exist.
- Add a check to see if the database already contains Album, Genre or Artists data. If it does, return early from the method.
- Add further code that adds 20 Genres, 20 Artists and 10 Albums to arrays and then commits them to the database. You should commit the array for each object type to the database before you create the next array.
When you generate the Albums, do you get an incomplete result, or no result? Why is that?! Did you try and add more than 10, or just 10 Albums?
You are hitting the token limit for GitHub Copilot's model Codex. Token limits include any prompt you send to the model asking it for a response. Have a read of this great post, paying attention to item number 8 on the list!
Tidy up your incomplete Albums array, or use Copilot to add new entries one at a time.
Previous - Adding a Data Source | Next - Updating Browse and Details Views