Database first approach using EF in asp.net core 3.0


In database first approach firstly we will create the database, on basis of database schema,CodeGeneration tool will create model class in application. This is one of the easy approach for doing CRUD operation in asp.net core.

Step 1:Create the database table like this

CREATE TABLE [dbo].[tblEmp](
[Id] [int] IDENTITY(1,1) NOT NULL,
[EmpName] [nvarchar](50) NULL,
[EmpAddress] [nvarchar](50) NULL,
[ContactNum] [nvarchar](50) NULL,
CONSTRAINT [PK_tblEmp] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

Step 2: Create the asp.net core 3.0 application using Visual studio 2019 like this

Step 3: Install the required NuGet packages like this

in the above package Microsoft.VisualStudio.Web.CodeGeneration.Design is used for generating controllers and views.

Microsoft.EntityFrameworkCore.Tools is used for creating database context and a model class from the database.

Microsoft.EntityFrameworkCore.SqlServer is used for communicating the sql server database using Entity-framework.

Asp.net core also support scaffolding which use t4 templates to generate the code for common functionalities like insert, update, fetch and delete.

Step 4: Run the scaffold command in Package Manager Console to reverse engineer the database to create database context and entity POCO classes from tables. The scaffold command will create POCO class only for the tables that have a primary key.

Scaffold-DbContext “Server=localhost;Database=Inventory;Integrated Security=True” Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

if we have to run this command on database update scenario then we can run like this

Scaffold-DbContext “Server=localhost;Database=Inventory;Integrated Security=True” Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -force

this command will create the model like this

Step 5: Go to the context class and remove the connection string from that file and move in appsettings.json file like this

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "InventoryDatabase": "Server=localhost;Database=Inventory;Integrated Security=True"
  }
}

Step 6: Go to controller folder and create Emp controller like given below image

Step 7: Now scaffolding will create the all the required code in EmpController and view for crud operation in view folder like this

Step 8: Now run the application, you will see the CRUD functionality for Emp. in the above demo, we have created the CRUD operation without writing the single line of code.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.