HI WELCOME TO SIRIS

EF 6 Code-First Conventions

Leave a Comment

Conventions are sets of default rules which automatically configure a conceptual model based on your domain classes when working with 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 ForDescription
SchemaBy 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 Name1) 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 NameBy 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 columnEF 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 ColumnEF creates NotNull columns for Primary Key properties and non-nullable value type properties e.g. int, float, decimal, datetime etc.
DB Columns orderEF 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 DBBy default, all properties will map to the database. Use the [NotMapped] attribute to exclude property or class from DB mapping.
Cascade deleteEnabled by default for all types of relationships.
The following table list C# data type mapped with SQL Server data type.
C# Data TypeMapping to SQL Server Data Type
intint
stringnvarchar(Max)
decimaldecimal(18,2)
floatreal
byte[]varbinary(Max)
datetimedatetime
boolbit
bytetinyint
shortsmallint
longbigint
doublefloat
charNo mapping
sbyteNo mapping
(throws exception)
objectNo mapping
The following figure illustrates the conventions mapping with the database.
Entity Framework code-first conventions

Relationship Convention

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.

Complex Type Convention

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.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.