NuGet Packages
- Microsoft.EntityFrameworkCore.SqlServer
- Microsoft SQL Server database provider for Entity Framework Core.
- Microsoft.EntityFrameworkCore.Tools
- Entity Framework Core Tools for the NuGet Package Manager Console in Visual Studio.
Design
Code
- Models\Customer.cs
- Models\Product.cs
- Models\Order.cs
- Models\ProductOrder.cs
- Data\ContosPetsContext.cs
- Program.cs
Package Manager Console
- Add-Migration [Name]
- Creates a new migration class as per specified name with the
Up()
andDown()
methods.
- Creates a new migration class as per specified name with the
- Update-Database
- Executes the last migration file created by the
Add-Migration
command and applies changes to the database schema.
- Executes the last migration file created by the
String?
- nullable
ICollection
- Defines methods to manipulate generic collections.
- ICollection is a interface that represents a collection, it also contains strongly typed members
DataAnnotations
- DataAnnotations are used in MVC to validate the data.
- It works on Client-side as well as server side.
- There are some pre-defined data-annotation attributes which we use directly to validate the data
- [Required]
- the property should be not null or the property should not be a string type, which is either empty or whitespace
- [Column(TypeName = “[Datatype Condition]”]
- Condition of Column
DbContext
- DbContext
- DbContext is called context class in entity framework.
- DbContext is the primary class that is responsible for interacting with the database
Querying: Converts LINQ-to-Entities queries to SQL query and sends them to the database.
Change Tracking: Keeps track of changes that occurred on the entities after querying from the database.
Persisting Data: Performs the Insert, Update and Delete operations to the database, based on entity states.
Caching: Provides first level caching by default. It stores the entities which have been retrieved during the life time of a context class.
Manage Relationship: Manages relationships using CSDL, MSL and SSDL in Db-First or Model-First approach, and using fluent API configurations in Code-First approach.
Object Materialization: Converts raw data from the database into entity objects.
- DbSet
- The
DbSet
class represents an entity set that can be used for create, read, update, and delete operations. - The
DbContext
must include the DbSet type properties for the entities which map to database tables and views.
- The
Override
- The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.
- An override method provides a new implementation of the method inherited from a base class.
- The method that is overridden by an override declaration is known as the overridden base method.
- An override method must have the same signature as the overridden base method.
DbContextOptionsBuilder
- Provides a simple API surface for configuring DbContextOptions.
- Databases (and other extensions) typically define extension methods on this object that allow you to configure the database connection (and other options) to be used for a context.
- You can use DbContextOptionsBuilder to configure a context by overriding OnConfiguring(DbContextOptionsBuilder) or creating a DbContextOptions externally and passing it to the context constructor.
UseSqlServer
- UseSqlServer
- Configures the context to connect to a Microsoft SQL Server database, but without initially setting any DbConnection or connection string.
- The connection or connection string must be set before the DbContext is used to connect to a database.
Connection String Syntax
- Data Source
- To connect to a named instance of SQL Server, use the
server name\instance
name syntax. - You can also set the DataSource property of the
SqlConnectionStringBuilder
to the instance name when building a connection string. - The
DataSource
property of aSqlConnection
object is read-only.
- To connect to a named instance of SQL Server, use the
- Initial Catalog
- If the user name that is in the connection string has access to more then one database, you have to specify the database you want the connection string to connect to.
- Intergrated Security
- When
false
, User ID and Password are specified in the connection. - When
true
, the current Windows account credentials are used for authentication.
- When
- Connect Timeout
- The length of time (in seconds) to wait for a connection to the server before terminating the attempt and generating an error.
- Encrypt
- When true, SQL Server uses SSL encryption for all data sent between the client and server if the server has a certificate installed.
- TrustServerCertificate
- The
TrustServerCertificate
keyword is valid only when connecting to aSQL Server instance
with a valid certificate. - When
TrustServerCertificate
is set totrue
, the transport layer will useSSL
to encrypt the channel and bypass walking the certificate chain to validate trust.
- The
- ApplicationIntent
- The keyword ApplicationIntent can be specified in your connection string. The assignable values are ReadWrite or ReadOnly. The default is ReadWrite.
- When ApplicationIntent=ReadOnly, the client requests a read workload when connecting. The server enforces the intent at connection time, and during a USE database statement.
- The ApplicationIntent keyword does not work with legacy read-only databases.
- MultiSubnetFailover
- Always specify
MultiSubnetFailover=Yes
when connecting to a SQL Server 2012 availability group listener or SQL Server 2012 Failover Cluster Instance. MultiSubnetFailover enables faster failover for all Availability Groups and failover cluster instance in SQL Server 2012 and will significantly reduce failover time for single and multi-subnet Always On topologies. During a multi-subnet failover, the client will attempt connections in parallel. During a subnet failover, SQL Server Native Client will aggressively retry the TCP connection. - The MultiSubnetFailover connection property indicates that the application is being deployed in an availability group or Failover Cluster Instance, and that SQL Server Native Client will try to connect to the database on the primary SQL Server instance by trying to connect to all the IP addresses. When
MultiSubnetFailover=Yes
is specified for a connection, the client retries TCP connection attempts faster than the operating system’s default TCP retransmit intervals. This enables faster reconnection after failover of either an Always On Availability Group or an Always On Failover Cluster Instance, and is applicable to both single- and multi-subnet Availability Groups and Failover Cluster Instances.
- Always specify