Add a new folder named “Model” to the project. 6. In the Models folder, add a class named Product. 7. Add a few properties like Id, Name, Description, and Price to the product class. The product should also be of some kind and for that, a category model is defined and a CategoryId property is added to the product model. 8. Similarly, add a Category model with properties like Id, Name, Description, Price
Add a new folder named “Model” to the project.
6. In the Models folder, add a class named Product.
7. Add a few properties like Id, Name, Description, and Price to the product class. The product should also be of some kind and for that, a category model is defined and a CategoryId property is added to the product model.
8. Similarly, add a Category model with properties like Id, Name, Description, Price
Step by step
Solved in 3 steps
Add a new folder named Repository in the project and add an Interface name IProductRepository in that folder. Add the methods in the interface that performs CRUD operations for Product microservice.
Add a connection string in the appsettings.json file.
Open the Startup.cs file to add the SQL server db provider for EF Core. Add the code services.AddDbContext<ProductContext>(o => o.UseSqlServer(Configuration.GetConnectionString("ProductDB"))); under ConfigureServices method. Note that in the GetConnectionString method the name of the key of the connection string is passed that was added in appsettings file.
Instructions: Though the .NET Core API project has inbuilt support for EF Core and all the related dependencies are downloaded at the time of project creation and compilation which could be found under the SDK section in the project as shown below.
Microsoft.EntityFrameworkCore.SqlServer (2.1.1) should be the package inside the downloaded SDKs. If it is not present, it could be explicitly added to the project via Nuget Packages.
9. Add a new folder named DBContexts to the project.
10. Add a new class named ProductContext which includes the DbSet properties for Products and Categories. OnModelCreating is a method via which the master data could be seeded to the