Day3 FGF
Day3 FGF
Course
Day 3: Creating and Configuring Model
Creating and Configuring
Model
Abhishek Luv
Entity Types
• Including a DbSet of a type on your context means that it is included in EF Core's
model; we usually refer to such a type as an entity.
• EF Core can read and write entity instances from/to the database, and if you're using
a relational database, EF Core can create tables for your entities via migrations.
• By convention, types that are exposed in DbSet properties on your context are
included in the model as entities.
• Entity types that are specified in the OnModelCreating method are also included, as
are any types that are found by recursively exploring the navigation properties of
other discovered entity types.
In the code sample below, all types are included:
• Blog is included because it's exposed in a DbSet property on the context.
• Post is included because it's discovered via the Blog.Posts navigation property.
• AuditEntry because it is specified in OnModelCreating.
Including types in the Model
Excluding types from the model
Table name
By convention, each entity type will be set up to map to a database table with the same
name as the DbSet property that exposes the entity. If no DbSet exists for the given
entity, the class name is used.
Table Schema
When using a relational database, tables are by convention created in your database's
default schema. For example, Microsoft SQL Server will use the dbo schema (SQLite
does not support schemas).
You can configure tables to be created in a specific schema as follows:
Table Schema
Rather than specifying the schema for each table, you can also define the default
schema at the model level with the fluent API:
Table comments
You can set an arbitrary text comment that gets set on the database table, allowing you
to document your schema in the database:
Built-in Conventions
EF Core includes many model building conventions that are enabled by default. You can
find all of them in the list of classes that implement the IConvention interface.
For example:
• ForeignKey convention and KeyAddedConvention
• And many more
Configure models
• EF Core uses a metadata model to describe how the application's entity types are
mapped to the underlying database.
• This model is built using a set of conventions that look for common patterns.
• The model can then be customized using mapping attributes (also known as data
annotations) and/or calls to the ModelBuilder methods (also known as fluent API) in
OnModelCreating, both of which will override the configuration performed by
conventions.
Data Annotations to configure models
You can also apply certain attributes (known as Data Annotations) to your classes and
properties. Data annotations will override conventions, but will be overridden by Fluent
API configuration.
Fluent API to configure models
• You can override the OnModelCreating method in your derived context and use the
fluent API to configure your model.
• This is the most powerful method of configuration and allows configuration to be
specified without modifying your entity classes.
• Fluent API configuration has the highest precedence and will override conventions
and data annotations.
Grouping Fluent API configurations
To reduce the size of the OnModelCreating method all configuration for an entity type
can be extracted to a separate class implementing IEntityTypeConfiguration<TEntity>.
Using EntityTypeConfigurationAttribute
Rather than explicitly configuring the model in the OnModelCreating, an
EntityTypeConfigurationAttribute can instead be placed on the entity type such that EF
Core can find and use appropriate configuration. For example:
This attribute means that EF Core will use the specified IEntityTypeConfiguration
implementation whenever the Book entity type is included in a model.
Thanks for joining!
Abhishek Luv