SlideShare a Scribd company logo
1 of 14
<1/>
Entity Framework Core
Prachi Patel
By
<2/>
Topics
Types of Migrations(Code First, DB First)
Data Annotation using
Fluent API
CRUD Implementation
<3/>
Difference Between…
The Code First approach begins with
defining your application's domain
model and generating the database
schema from it.
The Database First approach starts
with an existing database schema
and generates entity classes based
on it. Both approaches have their
advantages and can be used
depending on your specific project
requirements and preferences.
Approaches Approaches
<4/>
What is EF Core Used For?
EF Core can serve as an object-relational mapper (O/RM), which:
1. Enables .NET developers to work with a database using .NET objects.
2. Eliminates the need for most of the data-access code that typically needs to be written
The model(Entity classes + DBContext Objects)
With EF Core, data access is performed using a model.
A model is made up of entity classes and a context object that represents a session with the
database. The context object allows querying and saving data.
<5/>
Why is Data Annotations Used and
How it is Used?
Why? : To create database relationships configuration with Entity Framework Core we Convention – we
define relationships in classes itself.
e.g.For Primary key Convention is to look for a property named “Id” or one that combines the class
name and “Id”, such as “StudentId”.
Now let’s suppose Student class used the name StdntID instead of ID. When Code First does not
find a property that matches this convention, it will throw an exception because of Entity
Framework’s requirement that you must have a key property. You can use the key annotation to
specify which property is to be used as the EntityKey.
How?
Data Annotations – Provide attributes to properties to make if aware of key types
Fluent API - by scaffolding it generated classes (Code First Approach)
<6/>
All About Data Annotation
These are .NET attributes which can be applied on an entity class or properties to override default
conventions in EF 6 and EF Core.
are valid in both EF 6 and EF Core.
These attributes are not only used in Entity Framework but they can also be used with ASP.NET
MVC or data controls.
Data annotation attributes are included in the System.ComponentModel.DataAnnotations and
System.ComponentModel.DataAnnotations.Schema namespaces in EF 6 as well as in EF
Core.
<7/>
Data Annotation Attributes from
System.ComponentModel.DataAnnotations
Attribute Description
Key Can be applied to a property to specify a key property in an entity and make the
corresponding column a PrimaryKey column in the database.
Timestamp Can be applied to a property to specify the data type of a corresponding column in the
database as rowversion.
ConcurrencyCheck Can be applied to a property to specify that the corresponding column should be included in the
optimistic concurrency check.
Required Can be applied to a property to specify that the corresponding column is a NotNull column in the
database.
MinLength Can be applied to a property to specify the minimum string length allowed in the
corresponding column in the database.
MaxLength Can be applied to a property to specify the maximum string length allowed in the
corresponding column in the database.
StringLength Can be applied to a property to specify the maximum string length allowed in the
corresponding column in the database.
<8/>
System.ComponentModel.DataAnnotations.Schema
Attributes
Attribute Description
Table Can be applied to an entity class to configure the corresponding table name and schema in the
database.
Column Can be applied to a property to configure the corresponding column name, order and data type in
the database.
Index Can be applied to a property to configure that the corresponding column should have an Index in the
database. (EF 6.1 onwards only)
ForeignKey Can be applied to a property to mark it as a foreign key property.
NotMapped Can be applied to a property or entity class which should be excluded from the model and should
not generate a corresponding column or table in the database.
DatabaseGenerated Can be applied to a property to configure how the underlying database should generate the value for
the corresponding column e.g. identity, computed or none.
InverseProperty Can be applied to a property to specify the inverse of a navigation property that represents the other
end of the same relationship.
<9/>
CRUD in EF Core Web API
1. Add Model Class
using System.Collections.Generic;
namespace ABS.CodesAndLoads.Model
{
5 references I Prachi Patel. 161 days ago I 1 author. 1 change
public class LoadType
{
2 references ! PrachiPatel,187daysagoI1author.1change
public int LoadTypeId { get; set; }
2 references I Prachi Patel, 187 days ago I 1 author. 1 change
public string Code { get; set; }
2 references I Prachi Patel 187 dayys ago I 1 author. 1 change
public string Description { get; set; }
}
<10/>
CRUD in EF Core Web API
2. Add DBContext Class :
[using System.Text;
namespace ABS.CodesAndLoads.Model.Context
{16 references I Kiran Rohlt, 5 days ago 15 authors, 10 changes
public class CodesAndLoadsContext : DbContext
{
{0 references I Ray Gunial, 6 days ago I 1 author, 2 changes
public CodesAndLoadsContext(DbContextOptions<CodesAndLoadsContext> options):
:
base(options)
{
}
1 reference I Prachi Patel, 124 days ago I 1 author. 1 change
public DbSet<LoadType> LoadTypes { get; set; }
<11/>
CRUD in EF Core Web API
3. Register the database context
<12/>
CRUD in EF Core Web API
4. Create Or Update
<13/>
CRUD in EF Core Web API
5. Delete
<14/>
Thank you
For your attention

More Related Content

Similar to Entity Frame Work Core.pptx

Spring review_for Semester II of Year 4
Spring review_for Semester II of Year 4Spring review_for Semester II of Year 4
Spring review_for Semester II of Year 4than sare
 
Entity framework 4.0
Entity framework 4.0Entity framework 4.0
Entity framework 4.0Abhishek Sur
 
Building N Tier Applications With Entity Framework Services 2010
Building N Tier Applications With Entity Framework Services 2010Building N Tier Applications With Entity Framework Services 2010
Building N Tier Applications With Entity Framework Services 2010David McCarter
 
Entity framework and how to use it
Entity framework and how to use itEntity framework and how to use it
Entity framework and how to use itnspyre_net
 
How do i connect to that
How do i connect to thatHow do i connect to that
How do i connect to thatBecky Bertram
 
ASP.NET 3.5 SP1
ASP.NET 3.5 SP1ASP.NET 3.5 SP1
ASP.NET 3.5 SP1Dave Allen
 
Spring from a to Z
Spring from  a to ZSpring from  a to Z
Spring from a to Zsang nguyen
 
Struts Intro Course(1)
Struts Intro Course(1)Struts Intro Course(1)
Struts Intro Course(1)wangjiaz
 
Spring framework Controllers and Annotations
Spring framework   Controllers and AnnotationsSpring framework   Controllers and Annotations
Spring framework Controllers and AnnotationsAnuj Singh Rajput
 
Intake 38 data access 5
Intake 38 data access 5Intake 38 data access 5
Intake 38 data access 5Mahmoud Ouf
 
Microsoft Entity Framework
Microsoft Entity FrameworkMicrosoft Entity Framework
Microsoft Entity FrameworkMahmoud Tolba
 

Similar to Entity Frame Work Core.pptx (20)

Spring review_for Semester II of Year 4
Spring review_for Semester II of Year 4Spring review_for Semester II of Year 4
Spring review_for Semester II of Year 4
 
Entity framework 4.0
Entity framework 4.0Entity framework 4.0
Entity framework 4.0
 
Building N Tier Applications With Entity Framework Services 2010
Building N Tier Applications With Entity Framework Services 2010Building N Tier Applications With Entity Framework Services 2010
Building N Tier Applications With Entity Framework Services 2010
 
Struts N E W
Struts N E WStruts N E W
Struts N E W
 
Entity framework and how to use it
Entity framework and how to use itEntity framework and how to use it
Entity framework and how to use it
 
6 database
6 database 6 database
6 database
 
Entity Framework 4
Entity Framework 4Entity Framework 4
Entity Framework 4
 
J2EE pattern 5
J2EE pattern 5J2EE pattern 5
J2EE pattern 5
 
Intake 37 ef2
Intake 37 ef2Intake 37 ef2
Intake 37 ef2
 
MyBatis
MyBatisMyBatis
MyBatis
 
How do i connect to that
How do i connect to thatHow do i connect to that
How do i connect to that
 
ASP.NET 3.5 SP1
ASP.NET 3.5 SP1ASP.NET 3.5 SP1
ASP.NET 3.5 SP1
 
Spring from a to Z
Spring from  a to ZSpring from  a to Z
Spring from a to Z
 
PPT temp.pptx
PPT temp.pptxPPT temp.pptx
PPT temp.pptx
 
AAC Room
AAC RoomAAC Room
AAC Room
 
Entity framework
Entity frameworkEntity framework
Entity framework
 
Struts Intro Course(1)
Struts Intro Course(1)Struts Intro Course(1)
Struts Intro Course(1)
 
Spring framework Controllers and Annotations
Spring framework   Controllers and AnnotationsSpring framework   Controllers and Annotations
Spring framework Controllers and Annotations
 
Intake 38 data access 5
Intake 38 data access 5Intake 38 data access 5
Intake 38 data access 5
 
Microsoft Entity Framework
Microsoft Entity FrameworkMicrosoft Entity Framework
Microsoft Entity Framework
 

Recently uploaded

Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 

Recently uploaded (20)

Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 

Entity Frame Work Core.pptx

  • 2. <2/> Topics Types of Migrations(Code First, DB First) Data Annotation using Fluent API CRUD Implementation
  • 3. <3/> Difference Between… The Code First approach begins with defining your application's domain model and generating the database schema from it. The Database First approach starts with an existing database schema and generates entity classes based on it. Both approaches have their advantages and can be used depending on your specific project requirements and preferences. Approaches Approaches
  • 4. <4/> What is EF Core Used For? EF Core can serve as an object-relational mapper (O/RM), which: 1. Enables .NET developers to work with a database using .NET objects. 2. Eliminates the need for most of the data-access code that typically needs to be written The model(Entity classes + DBContext Objects) With EF Core, data access is performed using a model. A model is made up of entity classes and a context object that represents a session with the database. The context object allows querying and saving data.
  • 5. <5/> Why is Data Annotations Used and How it is Used? Why? : To create database relationships configuration with Entity Framework Core we Convention – we define relationships in classes itself. e.g.For Primary key Convention is to look for a property named “Id” or one that combines the class name and “Id”, such as “StudentId”. Now let’s suppose Student class used the name StdntID instead of ID. When Code First does not find a property that matches this convention, it will throw an exception because of Entity Framework’s requirement that you must have a key property. You can use the key annotation to specify which property is to be used as the EntityKey. How? Data Annotations – Provide attributes to properties to make if aware of key types Fluent API - by scaffolding it generated classes (Code First Approach)
  • 6. <6/> All About Data Annotation These are .NET attributes which can be applied on an entity class or properties to override default conventions in EF 6 and EF Core. are valid in both EF 6 and EF Core. These attributes are not only used in Entity Framework but they can also be used with ASP.NET MVC or data controls. Data annotation attributes are included in the System.ComponentModel.DataAnnotations and System.ComponentModel.DataAnnotations.Schema namespaces in EF 6 as well as in EF Core.
  • 7. <7/> Data Annotation Attributes from System.ComponentModel.DataAnnotations Attribute Description Key Can be applied to a property to specify a key property in an entity and make the corresponding column a PrimaryKey column in the database. Timestamp Can be applied to a property to specify the data type of a corresponding column in the database as rowversion. ConcurrencyCheck Can be applied to a property to specify that the corresponding column should be included in the optimistic concurrency check. Required Can be applied to a property to specify that the corresponding column is a NotNull column in the database. MinLength Can be applied to a property to specify the minimum string length allowed in the corresponding column in the database. MaxLength Can be applied to a property to specify the maximum string length allowed in the corresponding column in the database. StringLength Can be applied to a property to specify the maximum string length allowed in the corresponding column in the database.
  • 8. <8/> System.ComponentModel.DataAnnotations.Schema Attributes Attribute Description Table Can be applied to an entity class to configure the corresponding table name and schema in the database. Column Can be applied to a property to configure the corresponding column name, order and data type in the database. Index Can be applied to a property to configure that the corresponding column should have an Index in the database. (EF 6.1 onwards only) ForeignKey Can be applied to a property to mark it as a foreign key property. NotMapped Can be applied to a property or entity class which should be excluded from the model and should not generate a corresponding column or table in the database. DatabaseGenerated Can be applied to a property to configure how the underlying database should generate the value for the corresponding column e.g. identity, computed or none. InverseProperty Can be applied to a property to specify the inverse of a navigation property that represents the other end of the same relationship.
  • 9. <9/> CRUD in EF Core Web API 1. Add Model Class using System.Collections.Generic; namespace ABS.CodesAndLoads.Model { 5 references I Prachi Patel. 161 days ago I 1 author. 1 change public class LoadType { 2 references ! PrachiPatel,187daysagoI1author.1change public int LoadTypeId { get; set; } 2 references I Prachi Patel, 187 days ago I 1 author. 1 change public string Code { get; set; } 2 references I Prachi Patel 187 dayys ago I 1 author. 1 change public string Description { get; set; } }
  • 10. <10/> CRUD in EF Core Web API 2. Add DBContext Class : [using System.Text; namespace ABS.CodesAndLoads.Model.Context {16 references I Kiran Rohlt, 5 days ago 15 authors, 10 changes public class CodesAndLoadsContext : DbContext { {0 references I Ray Gunial, 6 days ago I 1 author, 2 changes public CodesAndLoadsContext(DbContextOptions<CodesAndLoadsContext> options): : base(options) { } 1 reference I Prachi Patel, 124 days ago I 1 author. 1 change public DbSet<LoadType> LoadTypes { get; set; }
  • 11. <11/> CRUD in EF Core Web API 3. Register the database context
  • 12. <12/> CRUD in EF Core Web API 4. Create Or Update
  • 13. <13/> CRUD in EF Core Web API 5. Delete