Tutorial by Examples

By default a property is a primary key if a property on a class is named “ID” (not case sensitive), or the class name followed by "ID". If the type of the primary key property is numeric or GUID it will be configured as an identity column. Simple Example: public class Room { // Pri...
You can remove any of the conventions defined in the System.Data.Entity.ModelConfiguration.Conventions namespace, by overriding OnModelCreating method. The following example removes PluralizingTableNameConvention. public class EshopContext : DbContext { public DbSet<Product> Products...
By default Code First includes in model Types defined as a DbSet property in context class. Reference types included in entity types even if they are defined in different assembly. Derived classes even if only the base class is defined as DbSet property Here is an example, that we are only...
By default Entity Framework maps decimal properties to decimal(18,2) columns in database tables. public class Box { public int Id { set; get; } public decimal Length { set; get; } public decimal Width { set; get; } public decimal Height { set; get; } } We can change the p...
Code First infer the relationship between the two entities using navigation property. This navigation property can be a simple reference type or collection type. For example, we defined Standard navigation property in Student class and ICollection navigation property in Standard class. So, Code Firs...
If class A is in relationship with class B and class B has property with the same name and type as the primary key of A, then EF automatically assumes that property is foreign key. public class Department { public int DepartmentId { set; get; } public string Name { set; get; } publi...

Page 1 of 1