Defining EF Core models
EF Core uses a combination of conventions, annotation attributes, and Fluent API statements to build an entity model at runtime, which enables any actions performed on the classes to later be automatically translated into actions performed on the actual database. An entity class represents the structure of a table, and an instance of the class represents a row in that table.First, we will review the three ways to define a model, with code examples, and then we will create some classes that implement those techniques.
Using EF Core conventions to define the model
The code we will write will use the following conventions:
- The name of a table is assumed to match the name of a
DbSet<T>
property in theDbContext
class, for example,Products
. - The names of the columns are assumed to match the names of properties in the entity model class, for example,
ProductId
. - The
string
.NET type is assumed to be anvarchar
type in the database. - The
int
.NET type is assumed...