


Unfortunately, controller generation needs a DBContext to work which our project doesn’t have, so we have to take the long way about to generate our views and then manually create a controller to go with them.
#ELECTRON API CALL CODE#
Start by adding the code generation package like we did above using the following command. Now that we have a model in our Electron project we need to create the views that go along with it.
#ELECTRON API CALL FULL#
The following is the full contact model class. This could be the point where a third project is added to allow the API and the Electron app to share common items, but just to keep the example simple I’m just going add a copy of the contact model from the API project to the Electron project. Over in the Electron project, we need a model to match the data the API is returning. With the code generation complete the API should be good to go.
#ELECTRON API CALL GENERATOR#
api tells the code generator this controller is for a REST style API and that no views should be generated.outDir is the directory the output will be in relative to the current directory of your command prompt.dataContext is the DB Context that will be used for the generation.model is the model class that will be used for the generation.name defines the name of the resulting controller.controller tells the code generator we are creating a controller.The following is a rundown of the ones used above. There is a lot of switches when using aspnet-codegenerator. dotnet aspnet-codegenerator controller -name ContactsController -model Contact -dataContext ContactsDbContext -outDir Controllers -api Now with the above package installed, we can use the following command to generate a controller with the CRUD operations already implemented. First, we need to add the .Design NuGet package to the API project using the following command in a command prompt set to the root of the API project. Instead of hand coding the controller we are going to use some code generation provided by Microsoft. The API is just going to handle the basic CRUD (create, read, update, delete) operations for contact. With our model and context setup, we can run the following two commands to add the initial migration and apply the migration to the database. Public ContactsDbContext(DbContextOptions options) public class ContactsDbContext : DbContext Next, is the DB Context, which is empty other than the DB Set for the contacts table. The model and DB Context of the API project match what was in the blog post I linked above, but I am going to include them here. Adding in Entity Framework Core ended up turning into a post of its own when you can read here. Now that the API project is created we need to add in the ability to interact with a database with Entity Framework Core. To create the API I used the following from the command prompt in the folder where I wanted the new project to be created. The code before any changes can be found here. This post will be expanding on the introduction to Electron.NET that I did here to add in a Web API hit to pull some data as well as the UI on the Electron side to use this data.
