Analyzing Entity Framework Code First default conventions

Akos Nagy
Nov 7, 2017

In my previous post I told the tale of how I used a wrong (but still awesome) tool to solve a problem. The problem was that I had not known the default conventions when using Entity Framework Code First.

To remedy this problem, I decided to take a look at the conventions runtime, generate the list of conventions and look at their source code at the Entity Framework Github repository.

So here's what I have found; might be useful for someone else too ;)

ModelNamespaceConventionThis convention uses the namespace of the derived class as the namespace of the conceptual model
ForeignKeyPrimitivePropertyAttributeConventionConvention to process instances of ForeignKeyAttribute found on foreign key properties in the model.
InversePropertyAttributeConventionConvention to process instances of InversePropertyAttribute found on navigation properties in the model.
IndexAttributeConventionConvention to process instances of IndexAttribute found on properties in the model.
ColumnAttributeConventionConvention to process instances of ColumnAttribute found on properties in the model.
StringLengthAttributeConventionConvention to process instances of StringLengthAttribute found on properties in the model.
MaxLengthAttributeConventionConvention to process instances of MaxLengthAttribute found on properties in the model.
DatabaseGeneratedAttributeConventionConvention to process instances of DatabaseGeneratedAttribute found on properties in the model.
ConcurrencyCheckAttributeConventionConvention to process instances of ConcurrencyCheckAttribute found on properties in the model.
TimestampAttributeConventionConvention to process instances of TimestampAttribute found on properties in the model.
RequiredNavigationPropertyAttributeConventionConvention to process instances of RequiredAttribute found on navigation properties in the model.
RequiredPrimitivePropertyAttributeConventionConvention to process instances of RequiredAttribute found on properties in the model.
KeyAttributeConventionConvention to process instances of KeyAttribute found on properties in the model.
NotMappedPropertyAttributeConventionConvention to process instances of NotMappedAttribute found on properties in the model.
TableAttributeConventionConvention to process instances of TableAttribute found on types in the model.
ComplexTypeAttributeConventionConvention to process instances of ComplexTypeAttribute found on types in the model.
NotMappedTypeAttributeConventionConvention to process instances of NotMappedTypeAttribute found on types in the model.
IdKeyDiscoveryConventionFinds the PK of every entity; derive and override MatchKeyProperty method to specify your own
AssociationInverseDiscoveryConventionConvention to detect navigation properties to be inverses of each other when only one pair of navigation properties exists between the related types
ForeignKeyNavigationPropertyAttributeConventionConvention to process instances of ForeignKeyAttribute found on navigation properties in the model.
OneToOneConstraintIntroductionConventionConvention to configure the primary key(s) of the dependent entity type as foreign key(s) in a one:one relationship.
NavigationPropertyNameForeignKeyDiscoveryConventionConvention to discover foreign key properties whose names are a combination of the dependent navigation property name and the principal type primary key property name(s).
PrimaryKeyNameForeignKeyDiscoveryConventionConvention to discover foreign key properties whose names match the principal type primary key property name(s).
TypeNameForeignKeyDiscoveryConventionConvention to discover foreign key properties whose names are a combination of the principal type name and the principal type primary key property name(s).
ForeignKeyAssociationMultiplicityConventionConvention to distinguish between optional and required relationships based on CLR nullability of the foreign key property.
OneToManyCascadeDeleteConventionGenerates FKs with cascade delete for one-to-many relationships
ComplexTypeDiscoveryConventionConfigures a type as a complex type if it has no primary key, no mapped base type and no navigation properties.
StoreGeneratedIdentityKeyConventionAdds the identity specification to int PKs
PluralizingEntitySetNameConventionConvention to set the entity set name to be a pluralized version of the entity type name.
DeclaredPropertyOrderingConventionMoves the PK to the top of the column list in tables
SqlCePropertyMaxLengthConventionSet a default length of 4000 for properties that support "length" if the provider is SqlCe
PropertyMaxLengthConventionSet a default length for string and binary types. If they are keys, a default length of 128 is applied, otherwise they are set to maxlength.
DecimalPropertyConventionSpecifies the default precision of decimal (18,2).
ModelContainerConventionUses the name of the DbContext as the container for the conceptual model
ManyToManyCascadeDeleteConventionGenerates FKs with cascade delete for many-to-many relationships
MappingInheritedPropertiesSupportConventionConvention to ensure an invalid/unsupported mapping is not created when mapping inherited properties (i.e. an inherited property cannot be mapped to two different tables by two different derived types)
PluralizingTableNameConventionConvention to set the table name to be a pluralized version of the entity type name.
ColumnOrderingConventionStrictGenerates the columns in order specified by the Column attribute; throws if two columns are specified the same values
ForeignKeyIndexConventionCreates a non-clustered index on FKs
Akos Nagy