Monday 20 November 2023

EF using Visual Studio Core Framework and VSCode

 I. DB First Approach using Visual Studio Core Framework:


Tools>>SQL Server>>New Query>>Select Local Server as 'MSSQLLocalDB'

create database OrderProcessingDB 

use OrderProcessingDB 


create table Customer

(CustId int constraint pk_cid primary key identity,

CustName varchar(30) not null,

CustAddr varchar(100) not null,

MobileNumber bigint constraint un_mno unique,

EmailID nvarchar(100) constraint un_email unique) 


create table Category

(CatId int constraint pk_catid primary key identity(100,100),

CatName varchar(100) not null) 


create table Product

(ProdID int constraint pk_pid primary key identity(10,1),

ProdName varchar(100) not null,

Price money not null,

QOH int not null,

CatId int constraint fk_cat_prod references Category(CatId))


create table Orders

(OrderID int constraint pk_oid primary key identity(10,2),

CustId int constraint fk_cust_ord references Customer(CustID),

ProdId int constraint fk_prod_ord references Product(ProdID),

Qty int not null,

TotalAmount money not null)


---Then

Create Class Library Project with .Net Standard & Core

Create Console Project with .Net Standard & Core


--Install 3 packages from nuget package:

>Microsoft.EntityFrameworkCore

>Microsoft.EntityFrameworkCore.SqlServer

>Microsoft.EntityFrameworkCore.Tools


--From Tools menu>nuget package manager>Package Manager Console:

>scaffold-dbcontext "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=OrderProcessingDB;Integrated Security=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models

OR

Scaffold-DbContext "Data Source=(localdb)\MSSqlLocalDB;Initial Catalog=ProductDB;Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models


II. DB First Approach using VSCode

#Open Cmd Prompt

>cd\
>cd C:\Subas\EF Training\
>mkdir EFCore_VSCodeDBFirstApp
>cd EFCore_VSCodeDBFirstApp 

--Or>cd C:\Subas\EF Training\EFCore_VSCodeDBFirstApp

--To create a Solution:
>dotnet new sln -n DBFirstExample
>dotnet new classlib -o DBFirstLibrary
>dotnet new console -o RefDBFirstLibrary

--To add project to a Solution:
>dotnet sln DBFirstExample.sln add DBFirstLibrary/DBFirstLibrary.csproj
>dotnet sln DBFirstExample.sln add RefDBFirstLibrary/RefDBFirstLibrary.csproj

#Open VSCode and select folder DBFirstLibrary
>cd DBFirstLibrary
--Goto Extensions>Install "C# for Visual Studio Code (powered by OmniSharp)".

>dotnet add package Microsoft.EntityFrameworkCore
>dotnet add package Microsoft.EntityFrameworkCore.SqlServer
>dotnet add package Microsoft.EntityFrameworkCore.Tools

>dotnet build

--(Gave Error to me)
>dotnet ef dbcontext scaffold "Data Source=(localdb)\MSSqlLocalDB;Initial Catalog=OrderProcessingDB;Integrated Security=true;" Microsoft.EntityFrameworkCore.SqlServer –o Models
--Move all model files to Models folder
--Move all DAL files to Controller folder
--Write the Business Logics

#Now Open new VSCode and select folder RefDBFirstLibrary and give reference to DBFirstLibrary project
--Open the Terminal
>cd..
>dotnet add RefDBFirstLibrary/RefDBFirstLibrary.csproj reference DBFirstLibrary/DBFirstLibrary.csproj
--Write the Business Logics in the Console

>dotnet build
>dotnet run

--Its update the changes made in the table to our domain classes in DBFirst Approach
>scaffold-dbcontext "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=OrderProcessingDB;Integrated Security=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models -f

III. Code First Approach using VSCode

#Open Cmd Prompt

>cd\
>cd C:\Subas\EF Training
>mkdir EFCore_VSCodeFirstApp
>cd EFCore_VSCodeFirstApp

--Or>cd C:\Subas\EF Training\EFCore_VSCodeFirstApp

>dotnet new sln -n CodeFirstSolution
>dotnet new classlib -o CodeFirstLib
>dotnet sln CodeFirstSolution.sln add CodeFirstLib/CodeFirstLib.csproj

#Open VSCode and select folder CodeFirstLib

--Create Model(Category.cs,Product.cs,Order.cs,CodeFirstDBContext.cs) under Models folder manually with all schema refernces

>cd CodeFirstLib

--Add the Packages
>dotnet add package Microsoft.EntityFrameworkCore
>dotnet add package Microsoft.EntityFrameworkCore.SqlServer
>dotnet add package Microsoft.EntityFrameworkCore.Tools

--Write the Business Logics

>dotnet build

--Add Migration-It will be create a snapshot of all Tables with datatypes
--VS2019/VS2022->add-migration CodeFirst_V1
--VS Code->dotnet ef migrations add <CodeFirst_V1>
--VS2019/VS2022->update-database
--VS Code->dotnet ef database update

--(Gave Error to me)--It will create Migration folder in the solution
>dotnet ef migrations add CodeFirst_V1

--It will update the snapshot of migration
>dotnet ef database update

No comments:

Post a Comment