.Net Core,  Microsoft

.net Core 2.1 and SQL via EF Core 2.1 -The revisit

It’s time to revisit this topic but for the latest up dates for .Net Core (v 2.1.1) and EF Core (v 2.1)

Let’s start with the common project using, in my case, VS 2017 15.7.4.  Start a .Net Project based on v 2.1.x (I currently have installed .Net Core SDK v 2.1.301
  (Gee think we have enough versions of things  😯 )

I’m doing a .Net Core Class so I can get my DBContext and Table models in place….
We start with some Packages:

Microsoft.EntityFrameworkCore.SqlServer (2.1.1)
Microsoft.EntityFrameworkCore.SqlServer.Design (1.1.5)
Microsoft.EntityFrameworkCore.Tools (2.1.1)

Next, time for a database and let’s Scaffold a DB Context and our models… On the .Net Core project start up the Package Manager Console.

dotnet ef dbcontext scaffold 
  "Server={ServerName};DataBase={DataBaseName};Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
   Microsoft.EntityFrameworkCore.SqlServer
   --output-dir Models
   --table {table1Name} --table {table1Name} --table {table1Name} .....
For Connection Strings to SQL with a user/pass, an example would be
"Server={ServerName;DataBase={DataBaseName}g;Integrated Security=False; User ID={sql user};Password={sql user Pass};
Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;
ApplicationIntent=ReadWrite;MultiSubnetFailover=False"

 

The Walk-Through…

Line 1 – dotnet ef dbcontext scaffold This is the start of the command. As we see dotnet is the actual start of the SDK utility. It is worth looking this up. “ef dbcontext scaffold” is how we start  the EF scaffold.
Line 2 – The Connection String **1
“Server={ServerName};DataBase={DataBaseName};Integrated Security=True;Connect   Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False”
Line 3 – This is the provider used for MS SQL server. Yes it would be different if another database was being used.
Line 4 – The –output param is giving us a way to make sure we put all models/dbcontext in a folder in the project.
Line 5 – The –table allows one to generate models (and DbContext) for only certain tables in the DB.
 

**1    This is the standard SQL connection string. This will take on various forms depending on what you are connecting to. And yes, one can use the localdb.

 

 

 

 

 

Leave a Reply