Wednesday 3 February 2021

Part 20 : Mini Project with Entity Framework Code First

# 10 minutes of reading In this article and few upcoming articles, I will show you mini project in Asp.Net Core 3.1. Here we will cover dependency injection pattern, repository pattern, EF concept, server/client side validation and Authentication. Sample code available on github
First create Asp.Net Core 3.1 application using MVC template shown below.
Create MobileBrand class in Models folder with below property
Add Context class which will used to communicate with database for reading and modifying data in database.
Open Startup file and update ConfigurationServices method highlighted below, MobileDbConnection is name of connection string configured in AppSettings.json file.
Open appSettings.json file and append below highlighted line for connection string. Make sure you entered connection string based on your machine.
Now time to create migration migration, Open Package manager console and run Add-Migration command shown below.
Migration folder gets created in root directory of your project. Now run below command to create database with MobileBrand table. All setting for creating database are done in MobileDbContext class.
Once update-Database command executed from package manager console, requested database gets created with tables defined in MobileDbContext class.
Create controller shown below.
Select below option while creating controller, with below option controller with all CRUD operation methods gets created (Index, Details, Create, Edit and Delete).
Controller gets added with all required method to perform CRUD operation.
By default, all the required Views for MobileBrandsController created in MobileBrands folder Under views.
Run application with below highlighted controller name on URL and check browser window. For now, there is not data available on database table, lets add records into database using Create New action.
Put some dummy data for create view of MobileBrands.
Data stored in database table and Index view gets render once you hit create button from above screen.
Previous Next

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home