In Part 3 of our adventure, we’ll explore database development with GitHub Copilot. Covering prerequisites and establishing context, we’ll demonstrate how to effectively utilize GitHub Copilot for creating and updating SQL scripts based on your database design.
Check out Part 1 & Part 2
If you haven’t done so already, be sure to check out Part 1 and Part 2, where I delve into GitHub Copilot products and share insights on how you can prepare to embark on this journey of building an MVP with GitHub Copilot.
So, let’s start by exploring how we can generate Entity Framework classes, followed by context classes, and then Database service classes using GitHub Copilot.
Here, we are going to use Code code-first approach of Entity framework development
Watch full video here
EF Standards Definition
Similar to how we initially created a standards file for SQL, we’ll now start by establishing a set of standards we want GitHub Copilot to adhere to when generating C# code for EF models and context classes. It’s important to note that these are just guidelines and standards; you can also reference other standard files like the .config file when generating code using GitHub Copilot.
Database Model Classes
The model classes in EF will mirror the tables defined in the SQL database. We can provide prompts referencing the table and then request to generate a model, as demonstrated below:
Can you generate an entity framework model class for tblCertifications.sql, follow #EFStandards.txt
The prompt above will generate a model class with fields corresponding to tblcertifications while adhering to the EF standards defined in the text file.
Similarly, below are the prompts to generate models for other tables.
EF Context Class
Now, let’s generate the context class. The context class is where all models are initialized, seed data is configured, and all other services connect to the database via the context class.
The prompt below generates the context class with default model mapping and default overridden methods.
Can you generate entity framework context 'ChaiCPContext' with code first approach.
Now add all other tables into the context file, i will use GitHub Copilot inline editor for this.
// Can you add DBSet for tblRoles
// Can you add DBSet for tblTechnology
You can continue to add similar prompts, refer one or multiple files to generate the DBset with relevant names.
Seed Data
Finally, as part of EF implementation, adding seed data into the tables is one of the tasks. We’ll explore how to generate seed data and then incorporate the logic into the context file using GitHub Copilot.
We’ll generate JSON data based on the created table structures, populated with real sample data. The prompt below demonstrates one method to generate data for, let’s say, tbltechnology.
Can you generate data for technology model
View the video below to see more examples of how to generate data for a various table of this project.
DB Services
Lastly, we’ll create the DB Service class, which connects to the EF Context service created earlier and then constructs methods to execute basic CRUD operations.
Conclusion
In this segment of the series, we explore the ease of generating Entity Framework implementations using GitHub Copilot. Be sure to watch the full video at the top to learn and master EF with GitHub Copilot.
In next part of this series, we will build Repository service on top of DB Services to write specific business logics.