PropertyBuilder Class

Definition

Provides a simple API for configuring a IMutableProperty.

Instances of this class are returned from methods when using the ModelBuilder API and it is not designed to be directly constructed in your application code.

public class PropertyBuilder : Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<Microsoft.EntityFrameworkCore.Metadata.IMutableModel>, Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<Microsoft.EntityFrameworkCore.Metadata.Internal.InternalPropertyBuilder>
public class PropertyBuilder : Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<Microsoft.EntityFrameworkCore.Metadata.Internal.InternalPropertyBuilder>
public class PropertyBuilder : Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<Microsoft.EntityFrameworkCore.Metadata.Builders.IConventionPropertyBuilder>
type PropertyBuilder = class
    interface IInfrastructure<IMutableModel>
    interface IInfrastructure<InternalPropertyBuilder>
type PropertyBuilder = class
    interface IInfrastructure<InternalPropertyBuilder>
type PropertyBuilder = class
    interface IInfrastructure<IConventionPropertyBuilder>
Public Class PropertyBuilder
Implements IInfrastructure(Of IMutableModel), IInfrastructure(Of InternalPropertyBuilder)
Public Class PropertyBuilder
Implements IInfrastructure(Of InternalPropertyBuilder)
Public Class PropertyBuilder
Implements IInfrastructure(Of IConventionPropertyBuilder)
Inheritance
PropertyBuilder
Derived
Implements
IInfrastructure<IMutableModel> IInfrastructure<Microsoft.EntityFrameworkCore.Metadata.Internal.InternalPropertyBuilder> IInfrastructure<IConventionPropertyBuilder>

Remarks

See Modeling entity types and relationships for more information.

Constructors

PropertyBuilder(IMutableProperty)

This is an internal API that supports the Entity Framework Core infrastructure and not subject to the same compatibility standards as public APIs. It may be changed or removed without notice in any release. You should only use it directly in your code with extreme caution and knowing that doing so can result in application failures when updating to a new Entity Framework Core release.

PropertyBuilder(InternalPropertyBuilder)

This API supports the Entity Framework Core infrastructure and is not intended to be used directly from your code. This API may change or be removed in future releases.

Properties

Metadata

The property being configured.

Methods

HasAnnotation(String, Object)

Adds or updates an annotation on the property. If an annotation with the key specified in annotation already exists its value will be updated.

HasConversion(Type)

Configures the property so that the property value is converted before writing to the database and converted back when reading from the database.

HasConversion(Type, Type)

Configures the property so that the property value is converted before writing to the database and converted back when reading from the database.

HasConversion(Type, ValueComparer)

Configures the property so that the property value is converted before writing to the database and converted back when reading from the database.

HasConversion(ValueConverter)

Configures the property so that the property value is converted to and from the database using the given ValueConverter.

HasConversion(ValueConverter, ValueComparer)

Configures the property so that the property value is converted to and from the database using the given ValueConverter.

HasConversion<TConversion,TComparer>()

Configures the property so that the property value is converted before writing to the database and converted back when reading from the database.

HasConversion<TConversion>()

Configures the property so that the property value is converted before writing to the database and converted back when reading from the database.

HasConversion<TConversion>(ValueComparer)

Configures the property so that the property value is converted before writing to the database and converted back when reading from the database.

HasField(String)

Sets the backing field to use for this property.

Backing fields are normally found by convention as described here: http://go.microsoft.com/fwlink/?LinkId=723277. This method is useful for setting backing fields explicitly in cases where the correct field is not found by convention.

By default, the backing field, if one is found or has been specified, is used when new objects are constructed, typically when entities are queried from the database. Properties are used for all other accesses. This can be changed by calling UsePropertyAccessMode(PropertyAccessMode).

HasMaxLength(Int32)

Configures the maximum length of data that can be stored in this property. Maximum length can only be set on array properties (including String properties).

HasPrecision(Int32)

Configures the precision of the property.

HasPrecision(Int32, Int32)

Configures the precision and scale of the property.

HasValueGenerator(Func<IProperty,IEntityType,ValueGenerator>)

Configures a factory for creating a ValueGenerator to use to generate values for this property.

Values are generated when the entity is added to the context using, for example, Add<TEntity>(TEntity). Values are generated only when the property is assigned the CLR default value (null for string, 0 for int, Guid.Empty for Guid, etc.).

This factory will be invoked once to create a single instance of the value generator, and this will be used to generate values for this property in all instances of the entity type.

This method is intended for use with custom value generation. Value generation for common cases is usually handled automatically by the database provider.

HasValueGenerator(Type)

Configures the ValueGenerator that will generate values for this property.

Values are generated when the entity is added to the context using, for example, Add<TEntity>(TEntity). Values are generated only when the property is assigned the CLR default value (null for string, 0 for int, Guid.Empty for Guid, etc.).

A single instance of this type will be created and used to generate values for this property in all instances of the entity type. The type must be instantiable and have a parameterless constructor.

This method is intended for use with custom value generation. Value generation for common cases is usually handled automatically by the database provider.

Setting null does not disable value generation for this property, it just clears any generator explicitly configured for this property. The database provider may still have a value generator for the property type.

HasValueGenerator<TGenerator>()

Configures the ValueGenerator that will generate values for this property.

Values are generated when the entity is added to the context using, for example, Add<TEntity>(TEntity). Values are generated only when the property is assigned the CLR default value (null for string, 0 for int, Guid.Empty for Guid, etc.).

A single instance of this type will be created and used to generate values for this property in all instances of the entity type. The type must be instantiable and have a parameterless constructor.

This method is intended for use with custom value generation. Value generation for common cases is usually handled automatically by the database provider.

HasValueGeneratorFactory(Type)

Configures the ValueGeneratorFactory for creating a ValueGenerator to use to generate values for this property.

Values are generated when the entity is added to the context using, for example, Add<TEntity>(TEntity). Values are generated only when the property is assigned the CLR default value (null for string, 0 for int, Guid.Empty for Guid, etc.).

A single instance of this type will be created and used to generate values for this property in all instances of the entity type. The type must be instantiable and have a parameterless constructor.

This method is intended for use with custom value generation. Value generation for common cases is usually handled automatically by the database provider.

Setting null does not disable value generation for this property, it just clears any generator explicitly configured for this property. The database provider may still have a value generator for the property type.

HasValueGeneratorFactory<TFactory>()

Configures the ValueGeneratorFactory for creating a ValueGenerator to use to generate values for this property.

Values are generated when the entity is added to the context using, for example, Add<TEntity>(TEntity). Values are generated only when the property is assigned the CLR default value (null for string, 0 for int, Guid.Empty for Guid, etc.).

A single instance of this type will be created and used to generate values for this property in all instances of the entity type. The type must be instantiable and have a parameterless constructor.

This method is intended for use with custom value generation. Value generation for common cases is usually handled automatically by the database provider.

Setting null does not disable value generation for this property, it just clears any generator explicitly configured for this property. The database provider may still have a value generator for the property type.

IsConcurrencyToken(Boolean)

Configures whether this property should be used as a concurrency token. When a property is configured as a concurrency token the value in the database will be checked when an instance of this entity type is updated or deleted during SaveChanges() to ensure it has not changed since the instance was retrieved from the database. If it has changed, an exception will be thrown and the changes will not be applied to the database.

IsRequired(Boolean)

Configures whether this property must have a value assigned or null is a valid value. A property can only be configured as non-required if it is based on a CLR type that can be assigned null.

IsRowVersion()

Configures the property as ValueGeneratedOnAddOrUpdate() and IsConcurrencyToken(Boolean).

Database providers can choose to interpret this in different way, but it is commonly used to indicate some form of automatic row-versioning as used for optimistic concurrency detection.

IsUnicode(Boolean)

Configures whether the property as capable of persisting unicode characters. Can only be set on String properties.

UsePropertyAccessMode(PropertyAccessMode)

Sets the PropertyAccessMode to use for this property.

By default, the backing field, if one is found by convention or has been specified, is used when new objects are constructed, typically when entities are queried from the database. Properties are used for all other accesses. Calling this method witll change that behavior for this property as described in the PropertyAccessMode enum.

Calling this method overrrides for this property any access mode that was set on the entity type or model.

UsePropertyAccessMode(PropertyAccessMode)

Sets the PropertyAccessMode to use for this property.

By default, the backing field, if one is found by convention or has been specified, is used when new objects are constructed, typically when entities are queried from the database. Properties are used for all other accesses. Calling this method will change that behavior for this property as described in the PropertyAccessMode enum.

Calling this method overrides for this property any access mode that was set on the entity type or model.

ValueGeneratedNever()

Configures a property to never have a value generated by the database when an instance of this entity type is saved.

ValueGeneratedOnAdd()

Configures a property to have a value generated only when saving a new entity, unless a non-null, non-temporary value has been set, in which case the set value will be saved instead. The value may be generated by a client-side value generator or may be generated by the database as part of saving the entity.

ValueGeneratedOnAddOrUpdate()

Configures a property to have a value generated when saving a new or existing entity.

ValueGeneratedOnUpdate()

Configures a property to have a value generated when saving an existing entity.

ValueGeneratedOnUpdateSometimes()

Configures a property to have a value generated under certain conditions when saving an existing entity.

Explicit Interface Implementations

IInfrastructure<IConventionPropertyBuilder>.Instance

The internal builder being used to configure the property.

IInfrastructure<IMutableModel>.Instance

The model that the property belongs to.

IInfrastructure<InternalPropertyBuilder>.Instance

The internal builder being used to configure the property.

Extension Methods

IsETagConcurrency(PropertyBuilder)

Configures this property to be the etag concurrency token.

ToJsonProperty(PropertyBuilder, String)

Configures the property name that the property is mapped to when targeting Azure Cosmos.

GetInfrastructure<T>(IInfrastructure<T>)

Gets the value from a property that is being hidden using IInfrastructure<T>.

This method is typically used by database providers (and other extensions). It is generally not used in application code.

IInfrastructure<T> is used to hide properties that are not intended to be used in application code but can be used in extension methods written by database providers etc.

HasColumnName(PropertyBuilder, String)

Configures the column that the property maps to when targeting a relational database.

HasColumnOrder(PropertyBuilder, Nullable<Int32>)

Configures the order of the column the property is mapped to.

HasColumnType(PropertyBuilder, String)

Configures the data type of the column that the property maps to when targeting a relational database. This should be the complete type name, including precision, scale, length, etc.

HasComment(PropertyBuilder, String)

Configures a comment to be applied to the column

HasComputedColumnSql(PropertyBuilder)

Configures the property to map to a computed column when targeting a relational database.

When called with no arguments, this method tells EF that a column is computed without needing to specify the actual SQL used to computed it. This can be useful when mapping EF to an existing database.

HasComputedColumnSql(PropertyBuilder, String)

Configures the property to map to a computed column when targeting a relational database.

HasComputedColumnSql(PropertyBuilder, String, Nullable<Boolean>)

Configures the property to map to a computed column when targeting a relational database.

HasDefaultValue(PropertyBuilder)

Configures the default value for the column that the property maps to when targeting a relational database.

When called with no argument, this method tells EF that a column has a default value constraint of some sort without needing to specify exactly what it is. This can be useful when mapping EF to an existing database.

HasDefaultValue(PropertyBuilder, Object)

Configures the default value for the column that the property maps to when targeting a relational database.

HasDefaultValueSql(PropertyBuilder)

Configures the default value expression for the column that the property maps to when targeting a relational database.

When called with no argument, this method tells EF that a column has a default value constraint of some sort without needing to specify exactly what it is. This can be useful when mapping EF to an existing database.

HasDefaultValueSql(PropertyBuilder, String)

Configures the default value expression for the column that the property maps to when targeting a relational database.

IsFixedLength(PropertyBuilder, Boolean)

Configures the property as capable of storing only fixed-length data, such as strings.

UseCollation(PropertyBuilder, String)

Configures the property to use the given collation. The database column will be created with the given collation, and it will be used implicitly in all collation-sensitive operations.

ForSqliteHasDimension(PropertyBuilder, Ordinates)

Configures the dimension of the column that the property maps to when targeting SQLite.

ForSqliteHasDimension(PropertyBuilder, Ordinates)
Obsolete.

Configures the dimension of the column that the property maps to when targeting SQLite.

HasGeometricDimension(PropertyBuilder, Ordinates)

Configures the dimension of the column that the property maps to when targeting SQLite.

ForSqliteHasColumnName(PropertyBuilder, String)

Configures the column that the property maps to when targeting SQLite.

ForSqliteHasColumnType(PropertyBuilder, String)

Configures the data type of the column that the property maps to when targeting SQLite.

ForSqliteHasDefaultValue(PropertyBuilder, Object)

Configures the default value for the column that the property maps to when targeting SQLite.

ForSqliteHasDefaultValueSql(PropertyBuilder, String)

Configures the default value expression for the column that the property maps to when targeting SQLite.

ForSqliteHasSrid(PropertyBuilder, Int32)
Obsolete.

Configures the SRID of the column that the property maps to when targeting SQLite.

HasSrid(PropertyBuilder, Int32)

Configures the SRID of the column that the property maps to when targeting SQLite.

ForSqlServerHasColumnName(PropertyBuilder, String)

Configures the column that the property maps to when targeting SQL Server.

ForSqlServerHasColumnType(PropertyBuilder, String)

Configures the data type of the column that the property maps to when targeting SQL Server. This should be the complete type name, including precision, scale, length, etc.

ForSqlServerHasComputedColumnSql(PropertyBuilder, String)

Configures the property to map to a computed column when targeting SQL Server.

ForSqlServerHasDefaultValue(PropertyBuilder, Object)

Configures the default value for the column that the property maps to when targeting SQL Server.

ForSqlServerHasDefaultValueSql(PropertyBuilder, String)

Configures the default value expression for the column that the property maps to when targeting SQL Server.

ForSqlServerUseSequenceHiLo(PropertyBuilder, String, String)
Obsolete.

Configures the key property to use a sequence-based hi-lo pattern to generate values for new entities, when targeting SQL Server. This method sets the property to be OnAdd.

IsSparse(PropertyBuilder, Boolean)

Configures whether the property's column is created as sparse when targeting SQL Server.

UseHiLo(PropertyBuilder, String, String)

Configures the key property to use a sequence-based hi-lo pattern to generate values for new entities, when targeting SQL Server. This method sets the property to be OnAdd.

UseIdentityColumn(PropertyBuilder, Int32, Int32)

Configures the key property to use the SQL Server IDENTITY feature to generate values for new entities, when targeting SQL Server. This method sets the property to be OnAdd.

UseIdentityColumn(PropertyBuilder, Int64, Int32)

Configures the key property to use the SQL Server IDENTITY feature to generate values for new entities, when targeting SQL Server. This method sets the property to be OnAdd.

UseSqlServerIdentityColumn(PropertyBuilder)

Configures the key property to use the SQL Server IDENTITY feature to generate values for new entities, when targeting SQL Server. This method sets the property to be OnAdd.

UseSqlServerIdentityColumn(PropertyBuilder, Int32, Int32)
Obsolete.

Configures the key property to use the SQL Server IDENTITY feature to generate values for new entities, when targeting SQL Server. This method sets the property to be OnAdd.

Applies to