StudentDetails Using CodeFirst Entity Framework Handson Solution Cognizant
In this post, we are going to cover all StudentDetails Using CodeFirst Handson that are asked under the Entity Framework Topic in Cognizant Solutions.
Introduction to Entity Framework, Building model with Database & Code first approach
StudentDetails Using CodeFirst
Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StudentManagement { public class Program { public static void Main(string[] args) { var s = new Student(); Console.WriteLine("Enter Student Id"); s.StudentId = int.Parse(Console.ReadLine()); Console.WriteLine("Enter Student Name"); s.StudentName = Console.ReadLine(); Console.WriteLine("Enter Department"); s.Department = Console.ReadLine(); Console.WriteLine("Enter Enrollment Date"); s.EnrolledDate = DateTime.ParseExact(Console.ReadLine(), "dd-MM-yyyy", null); Console.WriteLine("Enter PhoneNumber"); s.PhoneNumber = long.Parse(Console.ReadLine()); new Program().AddStudent(s); Console.WriteLine("Details Added Successfully"); } public void AddStudent(Student student) { using (var db = new CollegeContext()) { db.Students.Add(student); db.SaveChanges(); } } } }
Student.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StudentManagement { public class Student { public int StudentId { get; set; } public string StudentName { get; set; } public DateTime EnrolledDate { get; set; } public string Department { get; set; } public long PhoneNumber { get; set; } } }
CollegeContext.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.Entity; namespace StudentManagement { public class CollegeContext : DbContext { public DbSet<Student> Students { get; set; } public CollegeContext() : base("name=StudentConnectionString") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { } } }