Default Code-First Conventions in Entity Framework

Default Code-First Conventions in Entity Framework

In this article, I am going to discuss Default Code-First Conventions in Entity Framework with Examples. Please read our previous article where we discussed How to use Entity Framework Code First Approach before proceeding to this article. We are going to work with the same example that we created in our previous article.

Default Code-First Conventions in Entity Framework:

In Entity Framework Code-First Approach, the conventions are nothing but a set of default rules which automatically configure a Conceptual Model based on your domain classes. In our previous article, we created one example using Entity Framework Code-First Approach and we have seen that Entity Framework automatically configured the Primary Keys, the Foreign Keys, relationships between the tables, appropriate data types of the columns, etc. from the domain classes without any additional configurations.

And, this is possible because of the Entity Framework Code-First Conventions. That means the database schema will be configured based on the conventions by Entity Framework API Automatically. It is also possible to change these default conventions which are followed by Entity Framework and that we will discuss in our upcoming articles. For now, we need to focus on the Entity Framework Code-First Conventions.

What are the Default Entity Framework Code-First Conventions:

Let us understand the Default Entity Framework Code-First Conventions. They are as follows:

Schema:

By default, the Entity Framework API creates all the database objects (tables, stored procedures, etc) with the dbo schema. If you verify that the two database tables are created with the dbo schema as shown in the below image.

What are the Default Entity Framework Code-First Conventions

Table Name:

The Entity Framework API will create the Database table with the entity class name suffixed by s i.e. <Entity Class Name> + ‘s’. For example, the Student domain class (entity) would map to the Students database table and the Standard domain class would map to the Standards database table and you can verify the same in the database as shown in the below image.

What are the Default Entity Framework Code-First Conventions

Primary Key Name:

The Entity Framework API will create a primary key column for the property named Id or <Entity Class Name> + “Id” (case insensitive). For example, we have created StudentId and StandardId properties in the Student and Standard domain classes. So, these two properties will be created as Primary Key columns in the corresponding database tables as shown in the below image.

What are the Default Entity Framework Code-First Conventions

Note: If both Id and <Entity Class Name> + “Id” properties are present in a single model class then it will create the Primary key based on the Id column only. If the model class does not have any property key property then Entity Framework will throw an exception.

Foreign Key Column Name:

By default, the Entity Framework will look for the Foreign Key Property with the same name as the Principal Entity Primary key name in the Dependent Entity. If the foreign key property does not exist in the Dependent Entity class, then Entity Framework will create a Foreign Key column in the Database table with <Dependent Navigation Property Name> + “_” + <Principal Entity Primary Key Property Name>. For example, as the Student Entity has the Standard Dependent Navigation Property, so the Entity Framework API will create Standard_StandardId as the foreign key column in the Students table as the Student entity does not contain a foreign key property for Standard as shown in the below image.

Default Code-First Conventions in Entity Framework with Examples

Null and Not Null Columns:

By default, the Entity Framework API will create a null column for all reference type properties and nullable primitive properties, for example, string, Nullable<int>, Student, and Standard (all class type properties). And, the Entity Framework will create Not Null columns for Primary Key properties and non-nullable value type properties, for example, int, float, decimal, DateTime, etc. For a better understanding, please have a look at the below image which shows Student Entity and the corresponding Students database table.

Default Code-First Conventions in Entity Framework with Examples

DB Columns Order:

The Entity Framework API will create the Database table columns in the same order as the properties are added in the entity class. However, the primary key columns would be moved to the first position in the table.

Properties Mapping to DB:

By default, all properties will map to the database table columns. If you do not want to map any property, then you need to use the [NotMapped] attribute to exclude the property from column mapping. We will discuss this in detail in our coming articles. The following diagram shows the list C# data type mapped with SQL Server data type.

Default Code-First Conventions in Entity Framework with Examples

Relationship Convention in Entity Framework Code First Approach:

The Entity Framework implements the One-to-Many relationship using the navigation property by default convention. Please click here to learn more about the relationships between entities in Entity Framework. Entity Framework does not include default conventions for One-to-One and Many-to-Many relationships. We need to configure them explicitly either using Fluent API or Data Annotation. We will discuss this concept in detail in our coming article.

This is the default Code First Conventions of Entity Framework. These conventions can be overridden using Data Annotation or Fluent API. And we will discuss this in detail in our coming articles.

In the next article, I am going to discuss Database Initialization in Entity Framework Code First Approach with Examples. Here, in this article, I try to explain Default Code-First Conventions in Entity Framework and I hope you enjoyed this Default Code-First Conventions in Entity Framework article. Please give your valuable feedback and suggestions about this article.

Registration Open For New Online Training

Enhance Your Professional Journey with Our Upcoming Live Session. For complete information on Registration, Course Details, Syllabus, and to get the Zoom Credentials to attend the free live Demo Sessions, please click on the below links.

Leave a Reply

Your email address will not be published. Required fields are marked *