Example


using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using static EFCoreSql.SchoolContext;

namespace EFCoreSql
{
    class SchoolContext : DbContext
    {
        public SchoolContext() : base()
        { }
        public class Student
        {
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]             
            public int StudentId { get; set; }
            public string Name { get; set; }
        }
        public class Course
        {
            public int CourseId { get; set; }
            public string CourseName { get; set; }
        }
        public DbSet<Student> Students { get; set; }
        public DbSet<Course> Courses { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseOneDB("Database=efcoredb; server=onedb;User ID=user1; Password=xxxx; Host=127.0.0.1; Protocol=onsoctcp; Service=8909; connectdatabase=no");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            int rowCount = 1;
            
            using (var schoolContext = new SchoolContext())
            {
                schoolContext.ChangeTracker.LazyLoadingEnabled = false;

                bool _canCo = schoolContext.Database.CanConnect(); 
                if (_canCo == true)
                    Console.WriteLine("OneDB database is good");
                else
                    Console.WriteLine("Not able to connect to OneDB database!!");

                bool _Created = schoolContext.Database.EnsureCreated();
                if (_Created == false)
                    Console.WriteLine("Database exist in the OneDB");
                else
                    Console.WriteLine("Database either got created or became the current database " +
                        "since connection string is using connectdatabase=no, and tables got " +
                        "created if it didn't exist in OneDB!!");

                List<Student> stds = new List<Student>()
                {
                    new Student(){StudentId=1},
                    new Student(){StudentId=2},
                    new Student(){StudentId=3}
                };

                Console.WriteLine("Deleting all records from students table...");
                schoolContext.Students.RemoveRange(stds);
                schoolContext.SaveChanges();

                Console.WriteLine("Selecting record(s) from students table...");
                var _students = schoolContext.Students.FromSqlRaw("Select * from students;");
                var counter = _students.GetEnumerator();
                bool hasRow = counter.MoveNext();
                if (hasRow is false)
                    Console.WriteLine("No records found - Expected");
                else
                {
                    Console.WriteLine("Records found - NOT Expected!!");
                    rowCount = 1;
                    foreach (var row in _students)
                    {
                        Console.WriteLine("Row #" + rowCount + " = StudentId : " + row.StudentId
                            + ", Name : " + row.Name);
                        rowCount++;
                    }
                }

                Console.WriteLine("Inserting 3 records into students table...");
                // Insert records into Students table.
                var std1 = new Student() { StudentId = 1, Name = "Sheshnarayan" };
                var std2 = new Student() { StudentId = 2, Name = "Vardaan" };
                var std3 = new Student() { StudentId = 3, Name = "Samriddhi" };

                schoolContext.Students.AddRange(std1, std2, std3);
                schoolContext.SaveChanges();

                Console.WriteLine("Selecting record(s) from students table...");
                _students = schoolContext.Students.FromSqlRaw("Select * from students;");
                counter = _students.GetEnumerator();
                hasRow = counter.MoveNext();
                if (hasRow is false)
                    Console.WriteLine("No records found - NOT Expected!!");
                else
                {
                    Console.WriteLine("Records found - Expected");
                    rowCount = 1;
                    foreach (var row in _students)
                    {
                        Console.WriteLine("Row #" + rowCount + " = StudentId : " + row.StudentId + ", Name : " + row.Name);
                        rowCount++;
                    }
                }
            }

            using (var updateschoolContext = new SchoolContext())
            {
                updateschoolContext.ChangeTracker.LazyLoadingEnabled = false;

                bool _Created = updateschoolContext.Database.EnsureCreated();
                if (_Created == false)
                    Console.WriteLine("Database exist in the OneDB");
                else
                    Console.WriteLine("Database either got created or became the " +
                        "current database since connection string is using " +
                        "connectdatabase=no, and tables got created if it didn't exist in OneDB!!");

                Console.WriteLine("Updating Name column value to Monika of Students table " +
                    "where StudentID=1...");
                // Update records into Students table.
                var std4 = new Student() { StudentId = 1, Name = "Monika" };

                updateschoolContext.Students.Update(std4);
                updateschoolContext.SaveChanges();

                Console.WriteLine("Selecting just updated record to ensure its updated correctly from students table...");
                var _students = updateschoolContext.Students.FromSqlRaw("Select * from students where studentid=1;");
                var counter = _students.GetEnumerator();
                bool hasRow = counter.MoveNext();
                if (hasRow is false)
                    Console.WriteLine("No records found - NOT Expected!!");
                else
                {
                    Console.WriteLine("Records found - Expected");
                    rowCount = 1;
                    foreach (var row in _students)
                    {
                        Console.WriteLine("Row #" + rowCount + " = StudentId : " + row.StudentId + ", Name : " + row.Name);
                        rowCount++;
                    }
                }
            }
        }
    }
}