Create Crud Application Using Code First Approach in Asp.net 6
Hi, I'm Jaimin, a front-end developer with counting years of experience in HTML5, CSS3, JavaScript, and React. I am passionate about creating user-friendly web experiences that look great and drive engagement. In my free time, I enjoy exploring new hiking trails and checking out local coffee shops. Let's work together to bring your web project to life!
Create a New Project: Open Visual Studio and create a new ASP.NET Core Web Application. Choose the "ASP.NET Core Web App" template, select the appropriate version (ASP.NET Core 6 in your case), and choose the "Web Application" template.
Install Entity Framework Core: Right-click on your project in Solution Explorer, select "Manage NuGet Packages," and install the
Microsoft.EntityFrameworkCore.SqlServerpackage.Create Model: Create a
Studentclass in theModelsfolder:using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace CodeFirstAproch.Models { public class Student { [Key] public int ID { get; set; } [Column("StudentName", TypeName ="varchar(100)")] public string Name { get; set; } [Column("StudentGender", TypeName ="varchar(100)")] public string Gender { get; set; } public int Age { get; set; } [Column("StudenStandard", TypeName ="varchar(50)")] public string Standard { get; set; } } }DbContext: Create a
DbContextclass in theDatafolder:using Microsoft.EntityFrameworkCore; namespace CodeFirstAproch.Models { public class StudentDbContext : DbContext { public StudentDbContext(DbContextOptions options) : base(options) { } public DbSet<Student> Students { get; set; } } }Configure DbContext in Startup: In the Program.cs file, configure the DbContext in the
ConfigureServicesmethod:"ConnectionStrings": { "conne": "Server=DESKTOP-R3TAG3F\\SQLEXPRESS;Database=CodeFirstAproach;Trusted_Connection=True;TrustServerCertificate=True;" },Ensure you have a connection string named "conne" in your
appsettings.jsonfile.Controller and Views: Right-click on the
Controllersfolder, select "Add" -> "Controller," choose "MVC Controller with views, using Entity Framework," select yourStudentmodel, and choose yourApplicationDbContext.Apply Migrations and Update Database: Open Package Manager Console set the default project to your web project, and run:
Add-Migration InitialCreate Update-DatabaseRun the Application: Press
F5or click on the "Start Debugging" button to run your application. Visithttps://localhost:5001/Studentsyour browser to see the CRUD operations.
These steps assume you have Visual Studio and the necessary SDKs installed. Adjust the steps based on your specific project structure or requirements.






