Thursday, May 14, 2020

Entity Framework 6 : Default Code First Conventions

EF 6 Code First Default Conventions :

1) Default Inheritance Type: Table Per Hierarchy (TPH)

2) foreign key properties

- Any property with the same data type as the principal primary key property and

- with a name that follows one of the following formats represents a FK for the relationship:

  • <navigation property name><principal primary key property name>',
  • '<principal class name><primary key property name>', or
  • '<principal primary key property name>'

3) Primary Key Convention

-property is a primary key if a property on a class is named “ID” (not case sensitive), or the class name followed by "ID".

4) If FK on the dependent entity is not nullable, then Code First sets cascade delete on the relationship.

- Nullable:

public int? CountryId { get; set; }

5) Type Discovery:

- you define a context class that derives from DbContext and exposes DbSet properties for the types that you want to be part of the model.


6) exclude a type from the model

-use the NotMapped attribute or the DbModelBuilder.Ignore fluent

7) No PK or FK: Its a complex type. Complex Type rules include:

- type does not have properties that reference entity types and

- is not referenced from a collection property on another type.

No comments:

Post a Comment