Conventions are sets of default rules which automatically configure a conceptual model based on your domain classes while using the code-first approach. As you have seen in the code-first example in the previous chapter, EF API configured PrimaryKeys, ForeignKeys, relationships, column data types etc. from the domain classes without any additional configurations. This is because of the EF code-first conventions. If they are followed in domain classes, then the database schema will be configured based on the conventions. These EF 6.x Code-First conventions are defined in the System.Data.Entity.ModelConfiguration.Conventions namespace.
The following table lists default code first conventions:
Default Convention For | Description |
---|---|
Schema | By default, EF creates all the DB objects into the dbo schema. |
Table Name | <Entity Class Name> + 's'
EF will create a DB table with the entity class name suffixed by 's' e.g. Student domain class (entity) would map to the Students table.
|
Primary key Name | 1) Id
2) <Entity Class Name> + "Id" (case insensitive) EF will create a primary key column for the property named Id or <Entity Class Name> + "Id" (case insensitive). |
Foreign key property Name | By default EF will look for the foreign key property with the same name as the principal entity primary key name.
If the foreign key property does not exist, then EF will create an FK column in the Db table with <Dependent Navigation Property Name> + "_" + <Principal Entity Primary Key Property Name> e.g. EF will create Grade_GradeId foreign key column in the Students table if the Student entity does not contain foreignkey property for Grade .
|
Null column | EF creates a null column for all reference type properties and nullable primitive properties e.g. string, Nullable<int>, Student, Grade (all class type properties) |
Not Null Column | EF creates NotNull columns for Primary Key properties and non-nullable value type properties e.g. int, float, decimal, datetime etc. |
DB Columns order | EF will create DB columns in the same order like the properties in an entity class. However, primary key columns would be moved first. |
Properties mapping to DB | By default, all properties will map to the database. Use the [NotMapped] attribute to exclude property or class from DB mapping. |
Cascade delete | Enabled by default for all types of relationships. |
The following table list C# data type mapped with SQL Server data type.
C# Data Type | Mapping to SQL Server Data Type |
---|---|
int | int |
string | nvarchar(Max) |
decimal | decimal(18,2) |
float | real |
byte[] | varbinary(Max) |
datetime | datetime |
bool | bit |
byte | tinyint |
short | smallint |
long | bigint |
double | float |
char | No mapping |
sbyte | No mapping
(throws exception) |
object | No mapping |
The following figure illustrates the conventions mapping with the database.
EF 6 infers the One-to-Many relationship using the navigation property by default convention. Visit Convention for One-to-Many relationship chapter for more information.
Note: EF 6 does not include default conventions for One-to-One and Many-to-Many relationships. You need to configure them either using Fluent API or DataAnnotation.
Code First creates the complex type for the class which does not include key property and also the primary key is not registered using data annotation attribute or Fluent API.
This was an overview of code first conventions. These conventions can be overridden using DataAnnotation or Fluent API.