Entity framework conventions | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

Entity framework conventions when we use entity framework in our application, there are some predefined conventions of entity framework for example primary key EF convention means we defined a primary key in a model class then model mapped database table with a auto increment field or identity field another example, if we creating on cascading database table (Using primary key and foreign key) and parent table is deleted then all the child tables will also deleted. All the entity framework conventions are implemented by a IConvention interface so in this tutorial I am discussing about various entity framework cnventions and how to override all these conventions.

Convention Types:

  1. Data context class and connection string name defined in web.config file is same.
  2. All the public properties defined in a model class are automatically mapped into database table fields (columns).
  3. When we define another class reference in a model class using virtual it will perform lazy loading.
  4. All non nullable types, nullable types are not required and required respectively.
  5. If we define primary key in model class then that field will be mapped into identity field in a database table.
  6. If we define model class using a singular noun then datacntext will uses plurized table


  1. Primary key field over a property declaration is compulsory to be defined in model class. (IdKeyDiscoveryConvention).
  1. Associations to other entities are discovered automatically and the foreign key columns are built by composing the foreign entity name and its primary key (AssociationInverseDiscoveryConvention, NavigationPropertyNameForeignKeyDiscoveryConvention, PrimaryKeyNameForeignKeyDiscoveryConvention and TypeNameForeignKeyDiscoveryConvention).
  1. Child entities are deleted from the database whenever their parent is, if the relation is set to required (OneToManyCascadeDeleteConvention and ManyToManyCascadeDeleteConvention).

For now, there is no way to add our own custom conventions. If we want to disable a convention, just remove its class from the DbModelBuilder instance in the OnModelCreating override.

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
//create tables with the same names as the entities, do not pluralize them 



We have already seen how to override the conventional table and column names.

//change the physical table name


public class Project


//change the physical column name


public Int32 ProjectId { get; set; }


For the connection string to use, EF lets us call a base constructor that receives a connection string.

public class ProjectsContext : DbContext


//use a different connection string

public ProjectsContext(): base("Name=SomeConnectionString") { }