[.NET R & R] Project Overview and API Basics
Tags: software, webdev
Published: 2021-12-07 21:32:38

---
title: [.NET R & R] Project Overview and API Basics 
published: true
description: 
tags: software, webdev,
//cover_image: https://direct_url_to_image.jpg
---

         -Project Over view 
         -Creating the dotnet solution
         -Adding VS code extensions
         -Creating a new c# class for the product
         -Adding the Db context class 
         -Creating an entity framework migration
         -Creating a class to seed the data 
         -Creating an API controller return a list of products
         -Using async methods when querying a database 
         -sabving our code 


###Project Overview 
This project will be creating full-stack an e-commerce website using .NET with React and Redux [.NET R & R] from scratch. 
Source: https://www.udemy.com/user/neil-cummings-2/


###Creating the dotnet solution
To begin the project it is important to start at the terminal / command line. Using the first few commands to set up the project. 
Create a template for a starter API at the terminal / command line. The command ``` dotnet new sln  ``` will create a solution file for the project. The solution file is a container for the code. Next command will be  ```dotnet new webapi -o API ```to create and name the API. 

Then it is time to open the project in IDE for this project I chose visual studio code a short cut to opening a project from the terminal into VS code is to type ``` code . ``` 

To start the project 
CD into the API then run 
CD API 
dotnet run 


```dotnet watch run ```
This will make it where the program will be automatically updated so that there is not a need to stop the program and restart the program. 



the output 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7f4espbchtc274itmk0.png)


###Adding VS code extensions

Here are some of the most helpful VS code extensions for creating this project. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1pa67ije1yje08yss10s.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gjv20jt67unl0o8y1z8f.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/299tsozxiiy5rxht5aym.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ytba97qgkhpss9hr1fu9.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xphz85acfxdy9y0uanvf.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/econrbfz6p8hbaomz2mo.png)





###Creating a new c# class for the product

Start buy creating a Entities Folder 
Then create a new c# class file called products 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ohkbtqta2j3gco4y1l6b.png)

then create the properties. 

```c#    
public class Product
    {

        public int Id { get; set; }

        public string Name { get; set; }

        public string Description { get; set; }

        public long Price { get; set; }

        public string PictureUrl { get; set; }

        public string Type { get; set; }

        public string Brand { get; set; }

        public int QuantityInStock { get; set; }    



    }

}

```

###Adding the Db context class 
Start by looking for the NuGet gallery and search for 
Microsoft.EntityFrameworkcore make sure to select the correct package. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jdj5ni681fneq8cp40l8.png)

Then create a new folder for Data and a new c# class StoreContext.cs 

within that file 

```c#
public class StoreContext : DbContext 
{
    public StoreContext(DbContextOptions)
        {

        }
    public DbSet<Product> Products { get; set; }
}
```


###Creating an entity framework migration


Creating a migration based on the code written so far. 
The tool that will help us do this is 
###Dotnet-ef
https://www.nuget.org/packages/dotnet-ef/

The command to install in the project 
```
dotnet tool install --global dotnet-ef --version 6.0.0

```

to see all the tools 

```
dotnet tool list -g

```

to use the tool in the project 

```
dotnet ef migrations add InitialCreate -o Data/Migrations

```

a new folder will be created to hold the migrations in. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2qzy3o1cwnvbnz7899yt.png)


to show the database information 
open the command panel and look for sqlite 
open database 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qyxnmlk4ore4t7p32il6.png)

A new list of options appear to view the database 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/acxjcz16n1p22568jb3l.png)


###Creating a class to seed the data 

Putting data into the project. 

Inside the data folder create a new class 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eie9vwqxf3we6smwvcnm.png)

The name of the new class will be DbInitializer
This class will be used to create a list of products then add them to the database. 

The point of this is if anything were to go wrong with the database it will be easy to drop the database and create a new one and add the same data back into it. 

###Next Up in the project 

React Basics!!