EntityFramework - Simple Code-First Example

CSharp · Console

/Program.cs

// @nuget: EntityFramework

using System;
using System.Linq;
using System.Data.Entity;
using System.Collections;
using System.Collections.Generic;

public class Program
{
	public static void Main()
	{
		using (var ctx = new SchoolContext())
        {
			var stud = new Student() { StudentName = "Bill" };

			ctx.Students.Add(stud);
			ctx.SaveChanges(); 
	
			Console.Write("Student saved successfully!");
			
			// The following displays Student table data - dotnetfiddle.net specific code
			FiddleHelper.WriteTable("Student", ctx.Students.ToList());
		}
	}
}

public class SchoolContext: DbContext 
{
	public SchoolContext() : base(FiddleHelper.GetConnectionStringSqlServer()) // passed dotnetfiddle specific connction string
	{
	}
            
	public DbSet<Student> Students { get; set; }
    public DbSet<Grade> Grades { get; set; }
}

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
    public DateTime? DateOfBirth { get; set; }
    public byte[]  Photo { get; set; }
    public decimal Height { get; set; }
    public float Weight { get; set; }
        
    public Grade Grade { get; set; }
}

public class Grade
{
    public int GradeId { get; set; }
    public string GradeName { get; set; }
    public string Section { get; set; }
    
    public ICollection<Student> Students { get; set; }
}
;