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; }