Property Mappings Using Fluent Api

In this Tutorial let us look at how to configure the model properties and their related database columns using the fluent API.  Using Fluent API you can change the ColumnName using HasColumnName method. Alter the Order in which Column Appears Using HasColumnOrder . Set the Colum DataType using HasColumnType,  Make Column as Nullable Using IsOptional,  Set it as Not Null using isRequired. Set the Column Length using HasMaxLength, isMaxLength, IsFixedLength. Control the Precision of Decimal Columns using HasPrecision.  You can also configure the auto-generated values using the HasDatabaseGeneratedOption.  In this tutorial, we will look at all these methods in detail

Property Mappings Using Fluent API

In the last tutorial, we used entitytypeconfiguration (Entity<TEntityType>) configure, the entity types. This object has a Property method. The Property method is used to configure attributes of the property of the entity.

The Property method returns the configuration object. The returned Configuration object is specific to the type being configured. For Example date property returns DateTimePropertyConfiguration object. Similarly StringPropertyConfiguration , DecimalPropertyConfiguration, BinaryPropertyConfiguration, PrimitivePropertyConfiguration are returned by the Property method depending on the data type

Entity Type Property Configuration
Entity Type Property Configuration

The Configuration object has several methods, which can be used to configure the Property as shown below.

For this example we will use the following Model class. The model class has Employee , department and Project domain models

Download the source code from GitHub

HasColumnName

This method is used to set the database column name. The following code sets the name of the column as employeeName for the property name.

HasColumnOrder

This method is used to set the order which column appears in the table. The following code Name property will appear first in the table.

HasColumnType

It is used to set the column Data Type of the property. By Convention Code First creates nvarchar(max) for string, int for integer, varbinary(max) for Byte array, bit for boolean etc. You can read about Code First Conventions from here

You can override the default behaviour using the HasColumnType method

IsOptional

IsOptional is used to configures the Property to Null. The following creates a nullable column for address property.

IsRequired

IsRequired method used to Configure the Property to Not Null. The following code creates a Non Nullable column for email property.

HasMaxLength

This method sets the maximum length of the property. In the following code Address column is created by EF with the length 100

IsMaxLength

Configures the property to maximum length allowed by the database. In the following code Column Remark is mapped to nvarchar(max) data type

IsFixedLength

Configures the property to be fixed Length. The Fixed Length must be specified using the HasMaxLength method. This method converts the nvarchar to nchar column type

Ignore

Use this method to disable mapping of the property to the database column. In the following code, NoOfEmployee column will not be created

HasPrecision

This method is used to configure the Precision and scale of Decimal properties.

IsConcurrencyToken

This method applied to a property which you want to participate in concurrency check

IsRowVersion

This method used to create the column with datatype rowversion. rowversion columns are used in concurrency check. This method can only be applied on byte[] array properties

Unicode

This method is used to specify that the string should be of Unicode or not.

HasDatabaseGeneratedOption

This method used to configure the database generated values. It has three options

  • DatabaseGeneratedOption.Identity
    The identity values are generated when the row is inserted.
  • DatabaseGeneratedOption.Computed
    The value is generated when the data is inserted or updated to the database
  • DatabaseGeneratedOption.None
    The value of the property is not generated by the database when the row is inserted or updated

The following code below disables the auto-generated identity values for the Primary key column ProjectID

HasParameterName

This method sets the name of the parameter to be used for this property in stored procedures

The final DbContext class is as follows

Download the source code from GitHub

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top