You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2.8 KiB

Entity Framework Core

Code First Approach

  • Create the code for DbContext and models
  • Run the migration script to generate the db tables
  • Tutorial
PM> add-migration InitialDb
PM> update-database
PM> remove-migration
PM> Drop-Database
PM> script-migration

Database First Approach (Reverse Engineering)

PM> Scaffold-DbContext "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\MitLanguage.mdb;" EntityFrameworkCore.Jet -OutputDir Models -DataAnnotations -Force

Connection String

  • ConnectionString

  • Using OleDb (EntityFrameworkCore.Jet.OleDb)

    • x64 : Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\myFolder\\MitLanguage.mdb;
    • x86 : Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Machine\\Database\\MitMc.mdb;
  • Using ODBC (EntityFrameworkCore.Jet.Odbc)

    • x64 : Driver={Microsoft Access Driver (*.mdb, *.accdb)}; Dbq = C:\\myFolder\\MitCustom.mdb; Exclusive=1; Uid = Admin; Pwd =;
    • x86 : Driver={Microsoft Access Driver (*.mdb)}; Dbq = C:\\myFolder\\MitCustom.mdb; Exclusive=1; Uid = Admin; Pwd =;
    • For x86, use ODBC Data Source (32-bit) to add the UserDSN driver for 32-bit platform

Sample Query

using EFCoreTest.Models;
using System.Text.Json;

using (var db = new CustomContext())
{
    ReelAutoChanger? reelAutoChanger = db.ReelAutoChanger.FirstOrDefault();
    string jsonString = JsonSerializer.Serialize(reelAutoChanger);
    Console.WriteLine(jsonString);
    Console.ReadKey();
}

using (var db = new MachineContext())
{
    List<MotorProfile> motorProfiles = db.MotorProfile.ToList();
    //IQueryable<MotorProfile> motorProfiles = db.MotorProfile.FromSqlRaw("Select * from MotorProfile");
    //var emp = context.Employee.FromSqlRaw("Select * from Employee").OrderBy(x => x.Name).ToList();
    string jsonString = JsonSerializer.Serialize(motorProfiles);
    Console.WriteLine(jsonString);
    Console.ReadKey();

    List<Motor> motors = db.Motor.Where(b => b.ModuleName == "PnP").ToList();
    jsonString = JsonSerializer.Serialize(motors);
    Console.WriteLine(jsonString);
    Console.ReadKey();
}