Entity Framework Core Flashcards
Entity Framework Core
Entity Framework Core is the new version of Entity Framework after EF 6.x. It is open-source, lightweight, extensible and a cross-platform version of Entity Framework data access technology.
Entity Framework is an Object/Relational Mapping (O/RM) framework. It is an enhancement to ADO.NET that gives developers an automated mechanism for accessing & storing the data in the database.
EF Core is intended to be used with .NET Core applications. However, it can also be used with standard .NET 4.5+ framework based applications.
EF Core Development Approaches
EF Core supports two development approaches 1) Code-First 2) Database-First. EF Core mainly targets the code-first approach and provides little support for the database-first approach because the visual designer or wizard for DB model is not supported as of EF Core 2.0.
In the code-first approach, EF Core API creates the database and tables using migration based on the conventions and configuration provided in your domain classes. This approach is useful in Domain Driven Design (DDD).
In the database-first approach, EF Core API creates the domain and context classes based on your existing database using EF Core commands. This has limited support in EF Core as it does not support visual designer or wizard.
EF Core Database Providers
Entity Framework Core uses a provider model to access many different databases. EF Core includes providers as NuGet packages which you need to install.
The following table lists database providers and NuGet packages for EF Core.
Database NuGet Package
SQL Server Microsoft.EntityFrameworkCore.SqlServer
MySQL MySql.Data.EntityFrameworkCore
PostgreSQL Npgsql.EntityFrameworkCore.PostgreSQL
SQLite Microsoft.EntityFrameworkCore.SQLite
SQL Compact EntityFrameworkCore.SqlServerCompact40
In-memory Microsoft.EntityFrameworkCore.InMemory
Install Entity Framework Core
Entity Framework Core can be used with .NET Core or .NET 4.6 based applications. Here, you will learn to install and use Entity Framework Core 2.0 in .NET Core applications using Visual Studio 2017.
EF Core is not a part of .NET Core and standard .NET framework. It is available as a NuGet package. You need to install NuGet packages for the following two things to use EF Core in your application:
EF Core DB provider
EF Core tools
for sql server
Microsoft.EntityFrameworkCore.SqlServer(make sure that it has the .NET symbol and the Author is Microsoft)
Notice that the provider NuGet package also installed other dependent packages such as Microsoft.EntityFrameworkCore.Relational and System.Data.SqlClient.
Alternatively, you can also install provider’s NuGet package using Package Manager Console. Go to Tools -> NuGet Package Manager -> Package Manager Console and execute the following command to install SQL Server provider package:
PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install EF Core Tools
Along with the DB provider package, you also need to install EF tools to execute EF Core commands. These make it easier to perform several EF Core-related tasks in your project at design time, such as migrations, scaffolding, etc.
EF Tools are available as NuGet packages. You can install NuGet package for EF tools depending on where you want to execute commands: either using Package Manager Console (PowerShell version of EF Core commands) or using dotnet CLI.
Install EF Core Tools for PMC
In order to execute EF Core commands from Package Manager Console, search for the Microsoft.EntityFrameworkCore.Tools package from NuGet UI
This will allow you to execute EF Core commands for scaffolding, migration etc. directly from Package Manager Console (PMC) within Visual Studio.
to execute EF Core commands from .NET Core’s CLI
Install EF Core Tools for dotnet CLI
If you want to execute EF Core commands from .NET Core’s CLI (Command Line Interface), first install the NuGet package Microsoft.EntityFrameworkCore.Tools.DotNet using NuGet UI.
After installing Microsoft.EntityFrameworkCore.Tools.DotNet package, edit the .csproj file by right clicking on the project in the Solution Explorer and select Edit .csproj. Add node as shown below. This is an extra step you need to perform in order to execute EF Core 2.0 commands from dotnet CLI in VS2017.
Exe netcoreapp2.0
Now, open the command prompt (or terminal) from the root folder of your project and execute EF Core commands from CLI starting with dotnet ef
Creating a Model for an Existing Database in Entity Framework Core
Here you will learn how to create the context and entity classes for an existing database in Entity Framework Core. Creating entity & context classes for an existing database is called Database-First approach.
EF Core does not support visual designer for DB model and wizard to create the entity and context classes similar to EF 6. So, we need to do reverse engineering using the Scaffold-DbContext command. This reverse engineering command creates entity and context classes (by deriving DbContext) based on the schema of the existing database.
Scaffold-DbContext Command
Use Scaffold-DbContext to create a model based on your existing database. The following parameters can be specified with Scaffold-DbContext in Package Manager Console:
Scaffold-DbContext [-Connection] [-Provider] [-OutputDir] [-Context] [-Schemas>] [-Tables>]
[-DataAnnotations] [-Force] [-Project] [-StartupProject] []
In Visual Studio, select menu Tools -> NuGet Package Manger -> Package Manger Console and run the following command:
PM> Scaffold-DbContext “Server=.\SQLExpress;Database=SchoolDB;
Trusted_Connection=True;” Microsoft.EntityFrameworkCore.SqlServer -OutputDir Model
In the above command, the first parameter is a connection string which includes three parts: DB Server, database name and security info. Here, Server=.\SQLExpress; refers to local SQLEXPRESS database server. Database=SchoolDB; specifies the database name “SchoolDB” for which we are going to create classes. Trusted_Connection=True; specifies the Windows authentication. It will use Windows credentials to connect to the SQL Server. The second parameter is the provider name. We use provider for the SQL Server, so it is Microsoft.EntityFrameworkCore.SqlServer. The -OutputDir parameter specifies the directory where we want to generate all the classes which is the Models folder in this case.
The above Scaffold-DbContext command creates entity classes for each table in the SchoolDB database and context class (by deriving DbContext) with Fluent API configurations for all the entities in the Models folder.
get-help scaffold-dbcontext –detailed
Use the following command to get the detailed help on Scaffold-DbContext command:
PM> get-help scaffold-dbcontext –detailed
The following is the generated Student entity class for the Student table.
using System;
using System.Collections.Generic;
namespace EFCoreTutorials.Models { public partial class Student { public Student() { StudentCourse = new HashSet(); }
public int StudentId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public int? StandardId { get; set; }
public Standard Standard { get; set; } public StudentAddress StudentAddress { get; set; } public ICollection StudentCourse { get; set; } } } The following is the SchoolDBContext class which you can use to save or retrieve data.
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
namespace EFCoreTutorials.Models { public partial class SchoolDBContext : DbContext { public virtual DbSet Course { get; set; } public virtual DbSet Standard { get; set; } public virtual DbSet Student { get; set; } public virtual DbSet StudentAddress { get; set; } public virtual DbSet StudentCourse { get; set; } public virtual DbSet Teacher { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { #warning To protect potentially sensitive information in your connection string, you should move it out of source code
. See http://go.microsoft.com/fwlink/?
LinkId=723263 for guidance on storing connection strings.
optionsBuilder.UseSqlServer(@”Server=.\SQLExpress
;Database=SchoolDB;Trusted_Connection=True;”);
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity(entity => { entity.Property(e => e.CourseName) .HasMaxLength(50) .IsUnicode(false); entity.HasOne(d => d.Teacher) .WithMany(p => p.Course) .HasForeignKey(d => d.TeacherId) .OnDelete(DeleteBehavior.Cascade) .HasConstraintName("FK_Course_Teacher"); }); modelBuilder.Entity(entity => { entity.Property(e => e.Description) .HasMaxLength(50) .IsUnicode(false); entity.Property(e => e.StandardName) .HasMaxLength(50) .IsUnicode(false); }); modelBuilder.Entity(entity => { entity.Property(e => e.StudentId).HasColumnName("StudentID"); entity.Property(e => e.FirstName) .HasMaxLength(50) .IsUnicode(false); entity.Property(e => e.LastName) .HasMaxLength(50) .IsUnicode(false); entity.HasOne(d => d.Standard) .WithMany(p => p.Student) .HasForeignKey(d => d.StandardId) .OnDelete(DeleteBehavior.Cascade) .HasConstraintName("FK_Student_Standard"); }); modelBuilder.Entity(entity => { entity.HasKey(e => e.StudentId); entity.Property(e => e.StudentId) .HasColumnName("StudentID") .ValueGeneratedNever(); entity.Property(e => e.Address1) .IsRequired() .HasMaxLength(50) .IsUnicode(false); entity.Property(e => e.Address2) .HasMaxLength(50) .IsUnicode(false); entity.Property(e => e.City) .IsRequired() .HasMaxLength(50) .IsUnicode(false); entity.Property(e => e.State) .IsRequired() .HasMaxLength(50) .IsUnicode(false); entity.HasOne(d => d.Student) .WithOne(p => p.StudentAddress) .HasForeignKey(d => d.StudentId) .HasConstraintName("FK_StudentAddress_Student"); }); modelBuilder.Entity(entity => { entity.HasKey(e => new { e.StudentId, e.CourseId }); entity.HasOne(d => d.Course) .WithMany(p => p.StudentCourse) .HasForeignKey(d => d.CourseId) .OnDelete(DeleteBehavior.ClientSetNull) .HasConstraintName("FK_StudentCourse_Course"); entity.HasOne(d => d.Student) .WithMany(p => p.StudentCourse) .HasForeignKey(d => d.StudentId) .HasConstraintName("FK_StudentCourse_Student"); }); modelBuilder.Entity(entity => { entity.Property(e => e.StandardId).HasDefaultValueSql("((0))"); entity.Property(e => e.TeacherName) .HasMaxLength(50) .IsUnicode(false); entity.HasOne(d => d.Standard) .WithMany(p => p.Teacher) .HasForeignKey(d => d.StandardId) .OnDelete(DeleteBehavior.Cascade) .HasConstraintName("FK_Teacher_Standard"); }); } } }
EF Core creates entity classes only for tables and not for StoredProcedures or Views.
EF Core creates entity classes only for tables and not for StoredProcedures or Views.
dotnet ef dbcontext scaffold
DotNet CLI
If you use dotnet command line interface to execute EF Core commands then open command prompt and navigate to the root folder and execute the following dotnet ef dbcontext scaffold command:
dotnet ef dbcontext scaffold “Server=.\SQLEXPRESS;Database=SchoolDB;
Trusted_Connection=True;” Microsoft.EntityFrameworkCore.SqlServer -o Models
use the Migration commands whenever you change the model
Once you have created the model, you must use the Migration commands whenever you change the model to keep the database up to date with the model.
Configuring a DbContext
https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext
Entity Framework Core: DbContext
The DbContext class is an integral part of Entity Framework. An instance of DbContext represents a session with the database which can be used to query and save instances of your entities to a database. DbContext is a combination of the Unit Of Work and Repository patterns.
DbContext in EF Core allows us to perform following tasks
Manage database connection Configure model & relationship Querying database Saving data to the database Configure change tracking Caching Transaction managemen
To use DbContext in our application
To use DbContext in our application, we need to create the class that derives from DbContext, also known as context class. This context class typically includes DbSet properties for each entity in the model. Consider the following example of context class in EF Core.
public class SchoolContext : DbContext { public SchoolContext() {
} protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { }
protected override void OnModelCreating(ModelBuilder modelBuilder) { } //entities public DbSet Students { get; set; } public DbSet Courses { get; set; } } In the example above, the SchoolContext class is derived from the DbContext class and contains the DbSet properties of Student and Course type. It also overrides the OnConfiguring and OnModelCreating methods. We must create an instance of SchoolContext to connect to the database and save or retrieve Student or Course data.
OnConfiguring() method
The OnConfiguring() method allows us to select and configure the data source to be used with a context using DbContextOptionsBuilder. Learn how to configure a DbContext class at here.
OnModelCreating() method
The OnModelCreating() method allows us to configure the model using ModelBuilder Fluent API.
DbContext Methods
Method Usage
Add Adds a new entity to DbContext with Added state and starts tracking it. This new entity data will be inserted into the database when SaveChanges() is called.
AddAsync Asynchronous method for adding a new entity to DbContext with Added state and starts tracking it. This new entity data will be inserted into the database when SaveChangesAsync() is called.
AddRange Adds a collection of new entities to DbContext with Added state and starts tracking it. This new entity data will be inserted into the database when SaveChanges() is called.
AddRangeAsync Asynchronous method for adding a collection of new entities which will be saved on SaveChangesAsync().
Attach Attaches a new or existing entity to DbContext with Unchanged state and starts tracking it.
AttachRange Attaches a collection of new or existing entities to DbContext with Unchanged state and starts tracking it.
Entry Gets an EntityEntry for the given entity. The entry provides access to change tracking information and operations for the entity.
Find Finds an entity with the given primary key values.
FindAsync Asynchronous method for finding an entity with the given primary key values.
Remove Sets Deleted state to the specified entity which will delete the data when SaveChanges() is called.
RemoveRange Sets Deleted state to a collection of entities which will delete the data in a single DB round trip when SaveChanges() is called.
SaveChanges Execute INSERT, UPDATE or DELETE command to the database for the entities with Added, Modified or Deleted state.
SaveChangesAsync Asynchronous method of SaveChanges()
Set Creates a DbSet that can be used to query and save instances of TEntity.
Update Attaches disconnected entity with Modified state and start tracking it. The data will be saved when SaveChagnes() is called.
UpdateRange Attaches a collection of disconnected entities with Modified state and start tracking it. The data will be saved when SaveChagnes() is called.
OnConfiguring Override this method to configure the database (and other options) to be used for this context. This method is called for each instance of the context that is created.
OnModelCreating Override this method to further configure the model that was discovered by convention from the entity types exposed in DbSet properties on your derived context.
DbContext Properties
Method Usage
ChangeTracker Provides access to information and operations for entity instances this context is tracking.
Database Provides access to database related information and operations for this context.
Model Returns the metadata about the shape of entities, the relationships between them, and how they map to the database.
Creating the Model
Entity Framework needs to have a model (Entity Data Model) to communicate with the underlying database. It builds a model based on the shape of your domain classes, the Data Annotations and Fluent API configurations.
The EF model includes three parts: conceptual model, storage model, and mapping between the conceptual and storage models. In the code-first approach, EF builds the conceptual model based on your domain classes (entity classes), the context class and configurations. EF Core builds the storage model and mappings based on the provider you use. For example, the storage model will be different for the SQL Server compared with DB2.
EF uses this model for CRUD (Create, Read, Update, Delete) operations to the underlying database.
Code first appraoch -create entity classes and context classes first
public class Student { public int StudentId { get; set; } public string Name { get; set; } }
public class Course { public int CourseId { get; set; } public string CourseName { get; set; } } Now, we need to create a context class by deriving the DbContext, as shown in the previous chapter. The following SchoolContext class is also called context class.
namespace EFCoreTutorials { public class SchoolContext : DbContext { public DbSet Students { get; set; } public DbSet Courses { get; set; }
protected override void OnConfiguring (DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(@" Server=.\SQLEXPRESS ;Database=SchoolDB;Trusted_Connection=True;"); } } }
The above context class includes two DbSet properties, for Student and Course, type which will be mapped to the Students and Courses tables in the underlying database. In the OnConfiguring() method, an instance of DbContextOptionsBuilder is used to specify which database to use. We have installed MS SQL Server provider, which has added the extension method UseSqlServer on DbContextOptionsBuilder.
The connection string “Server=.\SQLEXPRESS;Database=SchoolDB;
Trusted_Connection=True;” in the UseSqlServer method provides database information: Server= specifies the DB Server to use, Database= specifies the name of the database to create and Trusted_Connection=True specifies the Windows authentication mode. EF Core will use this connection string to create a database when we run the migration.
After creating the context and entity classes, it’s time to add the migration to create a database.