In this tutorial, we will see the naming conventions, default rules that the framework uses to map C# classes to database tables.
EF Core follows a set of conventions to generate names for tables, columns, primary keys, and foreign keys, which allow EF to infer the structure of the database.
If we follow these conventions, EF will generate the database schema without the need for additional configuration (although we can later customize this configuration).
Let’s look at the main ones,
Table Naming Convention
By default, EF Core uses the class name for the table name in the database.
That is, if you have a C# class called Product
, EF Core will automatically generate a table called Products
(in plural) in the database.
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
This will be mapped in the database to:
CREATE TABLE Products (
Id INT PRIMARY KEY,
Name NVARCHAR(MAX)
);
Column Naming Convention
By default, EF Core uses the property name of the class as the column name in the database.
For example, if the class Student
has a property called Name
, EF Core will generate a column called Name
in the corresponding table.
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
In this case, EF will create the columns Id
, Name
, and Age
in the Students
table.
- The types are inferred from the data type in C# (
string
→NVARCHAR
,int
→INT
, etc.).
Primary Key Naming Convention
EF Core has a convention for recognizing the primary key of each entity. If an entity has a property called Id
or the class name followed by Id
.
For example, if we have StudentId
for the class Student
, EF Core will use it as the primary key of the table.
public class Student
{
public int StudentId { get; set; } // Convention for the primary key
public string Name { get; set; }
public int Age { get; set; }
}
In this case, EF Core will automatically recognize the property StudentId
as the primary key of the Students
table.
EF follows these rules to identify primary keys:
- A property named
Id
or{ClassName}Id
is considered a primary key. - If it is of type
int
,Guid
, or similar, it will be configured as auto-incrementing.
Foreign Key and Relationship Naming Convention
When there are relationships between entities, EF Core will look for a property whose name is the name of the related entity followed by Id
.
For example, if you have a relationship between Student
and Course
, EF Core will look for a property called CourseId
to represent it as the foreign key.
public class Course
{
public int CourseId { get; set; }
public string Name { get; set; }
public ICollection<Student> Students { get; set; }
}
public class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public int CourseId { get; set; } // Convention for the foreign key
public Course Course { get; set; }
}
In this case, EF Core will recognize the property CourseId
in the Student
class as the foreign key that relates to the Courses
table.
How to Customize or Disable Conventions
We can modify the default behavior using Data Annotations or Fluent API (we will see this in upcoming tutorials).
Additionally, if we need to modify the default behavior globally, we can add or modify naming conventions.
protected override void ConfigureConventions(ModelConfigurationBuilder builder)
{
builder.Properties<string>()
.HaveMaxLength(100); // All string properties will have a maximum length of 100 characters
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Remove the pluralization convention
modelBuilder.Model.GetEntityTypes()
.ToList()
.ForEach(e => e.SetTableName(e.DisplayName()));
}