Entity Framework Core

This is a note based on https://docs.microsoft.com/en-us/ef/core/index.

1. EF Core Model

EF uses a set of conventions to build a model based on entity classes. The model building can be customized by additional configuration. You can use either fluent API or data annotations to configure the model. The ModelBuilder Fluent API is prefered because it is more powerful and unintrusive than the data annontation method.

By convention, types that are exposed in DbSet properties on your context and types mentioned in onModelCreating method are included in the database model. Use modelBuilder.Ignore<MyEntityType>() to exclude a type.

By convention, public properties with a getter and a setter will be include in the model. Use modelBuilder.Entity<MyEntityType>.Ignore(e => e.PropertyToBeIgnored) to exclude a property.

By convention, a property named id or <type name>Id will be configured as the key of an entity. Use HasKey() to specify a property (or multiple properties) as the key (or composite key) of an entity.

By convention, primary keys that are of an integer or GUID data type will have values generated on add (DatabaseGeneratedOption.Identity). All other properties will be setup with no value generation (DatabaseGeneratedOption.None). With fluent api, you can configure the generation type using one of the three methods: .ValueGeneratedNever(), ValueGeneratedOnAdd(), or ValueGeneratedOnAddOrUpdate() (for the type DatabaseGeneratedOption.Computed).

By convention, a property whose CLR type cannot contain null wil be configured as required. Nullable types are configured as optional. Use IsRequired to make it as required.

By convention, a database provider uses a data type for the longest length of the CLR data type. For example, Microsoft SQL Server will use nvarchar(max) for string properties or nvarchar(450) if the column is used as a key. Use HasMaxLength() method to set a max length.

For concurrency control or row version, use IsConcurrencyToken() method to add a concurrency token to the model and use IsRowVersion() to configure a timestamp. A timestamp is also used as a concurrency token.

By convention, when a relationship is discovered but no foreign key property is found in the dependent entity clas, a shadow foreign key property will be created with a name pattern of <navigation property name><principal key property name>. Use Property<DataType>("PropertyName") to create a shadow property.

By convention, a relationship will be created when ther is a navigation property discovered on a type. A property is considered a navigation property if it points to cannot be mapped as a scalar type by the current database provider. Navigation properties can be defined in both sides or in one side. If no foreign key defined in the dependent entity, a shadow foreign key property will be created. By convention, cascade delete will be set to Cascade for requred relationships and ClientSetNull for optional relationships. to configure a relationship, you start by identifying the navigation properties that make up the relationship. HasOne or HasMany identifies the navigation property on the entity type you are working on. WithOne or WithMany to identify the inverse navigation. A parameterless WithOne or WithMany indidates that there is conceptually a refernce or collection on the other end of the relationship. Use HasForeignKey to define the foreign key property. Use HasPrincipalKey to use an alternate key of the principal. Use IsRequired to configure whether the relationship (and the foreign key) is required or optional. Use OnDelete to configure cascade delete behavior. When define one-to-one relationship, you need to specify the dependent entity type using HasForeignKey<DependentType>.

By convention, an index is created for each foreign key property. Use HasIndex to create an index for a property. By default, it’s non-unique, use IsUnique to make it unique.

By convention, an alternative key is introduced when a property is configured in HasPrincipalKey. Use HasAlternativeKey to explicitly define one.

By convention, the following fields will be discovered as backing fields for a given property.

_<camel-cased property name>
_<property name>
m_<camel-cased property name>
m_<property name>

Use HasField, UsePropertyAccessMode, Property to configure a backing field.

Uee IModelCacheKeyFactory to use context property in model creating.

2. RDB Modeling

Written on November 6, 2017