Class PropertyBuilder<TProperty>
- Namespace
- Microsoft.EntityFrameworkCore.Metadata.Builders
- Assembly
- Microsoft.EntityFrameworkCore.dll
Provides a simple API for configuring a IMutableProperty.
public class PropertyBuilder<TProperty> : PropertyBuilder, IInfrastructure<IConventionPropertyBuilder>
Type Parameters
TProperty
- Inheritance
-
PropertyBuilder<TProperty>
- Implements
- Inherited Members
- Extension Methods
Remarks
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.
See Modeling entity types and relationships for more information and examples.
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.
[EntityFrameworkInternal]
public PropertyBuilder(IMutableProperty property)
Parameters
property
IMutableProperty
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.
public virtual PropertyBuilder<TProperty> HasAnnotation(string annotation, object? value)
Parameters
annotation
stringThe key of the annotation to be added or updated.
value
objectThe value to be stored in the annotation.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
HasConversion(ValueConverter?)
Configures the property so that the property value is converted to and from the database using the given ValueConverter.
public virtual PropertyBuilder<TProperty> HasConversion(ValueConverter? converter)
Parameters
converter
ValueConverterThe converter to use.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
HasConversion(ValueConverter?, ValueComparer?)
Configures the property so that the property value is converted to and from the database using the given ValueConverter.
public virtual PropertyBuilder<TProperty> HasConversion(ValueConverter? converter, ValueComparer? valueComparer)
Parameters
converter
ValueConverterThe converter to use.
valueComparer
ValueComparerThe comparer to use for values before conversion.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
HasConversion(ValueConverter?, ValueComparer?, ValueComparer?)
Configures the property so that the property value is converted to and from the database using the given ValueConverter.
public virtual PropertyBuilder<TProperty> HasConversion(ValueConverter? converter, ValueComparer? valueComparer, ValueComparer? providerComparer)
Parameters
converter
ValueConverterThe converter to use.
valueComparer
ValueComparerThe comparer to use for values before conversion.
providerComparer
ValueComparerThe comparer to use for the provider values.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
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.
public virtual PropertyBuilder<TProperty> HasConversion(Type? providerClrType)
Parameters
providerClrType
TypeThe type to convert to and from or a type that inherits from ValueConverter.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
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.
public virtual PropertyBuilder<TProperty> HasConversion(Type conversionType, ValueComparer? valueComparer)
Parameters
conversionType
TypeThe type to convert to and from or a type that inherits from ValueConverter.
valueComparer
ValueComparerThe comparer to use for values before conversion.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
HasConversion(Type, ValueComparer?, ValueComparer?)
Configures the property so that the property value is converted before writing to the database and converted back when reading from the database.
public virtual PropertyBuilder<TProperty> HasConversion(Type conversionType, ValueComparer? valueComparer, ValueComparer? providerComparer)
Parameters
conversionType
TypeThe type to convert to and from or a type that inherits from ValueConverter.
valueComparer
ValueComparerThe comparer to use for values before conversion.
providerComparer
ValueComparerThe comparer to use for the provider values.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
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.
public virtual PropertyBuilder<TProperty> HasConversion(Type conversionType, Type? comparerType)
Parameters
conversionType
TypeThe type to convert to and from or a type that inherits from ValueConverter.
comparerType
TypeA type that inherits from ValueComparer.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
HasConversion(Type, 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.
public virtual PropertyBuilder<TProperty> HasConversion(Type conversionType, Type? comparerType, Type? providerComparerType)
Parameters
conversionType
TypeThe type to convert to and from or a type that inherits from ValueConverter.
comparerType
TypeA type that inherits from ValueComparer.
providerComparerType
TypeA type that inherits from ValueComparer to use for the provider values.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
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.
public virtual PropertyBuilder<TProperty> HasConversion<TConversion>()
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
Type Parameters
TConversion
The type to convert to and from or a type that inherits from ValueConverter.
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.
public virtual PropertyBuilder<TProperty> HasConversion<TConversion>(ValueComparer? valueComparer)
Parameters
valueComparer
ValueComparerThe comparer to use for values before conversion.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
Type Parameters
TConversion
The type to convert to and from or a type that inherits from ValueConverter.
HasConversion<TConversion>(ValueComparer?, ValueComparer?)
Configures the property so that the property value is converted before writing to the database and converted back when reading from the database.
public virtual PropertyBuilder<TProperty> HasConversion<TConversion>(ValueComparer? valueComparer, ValueComparer? providerComparer)
Parameters
valueComparer
ValueComparerThe comparer to use for values before conversion.
providerComparer
ValueComparerThe comparer to use for the provider values.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
Type Parameters
TConversion
The type to convert to and from or a type that inherits from ValueConverter.
HasConversion<TProvider>(ValueConverter<TProperty, TProvider>?)
Configures the property so that the property value is converted to and from the database using the given ValueConverter<TModel, TProvider>.
public virtual PropertyBuilder<TProperty> HasConversion<TProvider>(ValueConverter<TProperty, TProvider>? converter)
Parameters
converter
ValueConverter<TProperty, TProvider>The converter to use.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
Type Parameters
TProvider
The store type generated by the converter.
HasConversion<TProvider>(ValueConverter<TProperty, TProvider>?, ValueComparer?)
Configures the property so that the property value is converted to and from the database using the given ValueConverter<TModel, TProvider>.
public virtual PropertyBuilder<TProperty> HasConversion<TProvider>(ValueConverter<TProperty, TProvider>? converter, ValueComparer? valueComparer)
Parameters
converter
ValueConverter<TProperty, TProvider>The converter to use.
valueComparer
ValueComparerThe comparer to use for values before conversion.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
Type Parameters
TProvider
The store type generated by the converter.
HasConversion<TProvider>(ValueConverter<TProperty, TProvider>?, ValueComparer?, ValueComparer?)
Configures the property so that the property value is converted to and from the database using the given ValueConverter<TModel, TProvider>.
public virtual PropertyBuilder<TProperty> HasConversion<TProvider>(ValueConverter<TProperty, TProvider>? converter, ValueComparer? valueComparer, ValueComparer? providerComparer)
Parameters
converter
ValueConverter<TProperty, TProvider>The converter to use.
valueComparer
ValueComparerThe comparer to use for values before conversion.
providerComparer
ValueComparerThe comparer to use for the provider values.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
Type Parameters
TProvider
The store type generated by the converter.
HasConversion<TProvider>(Expression<Func<TProperty, TProvider>>, Expression<Func<TProvider, TProperty>>)
Configures the property so that the property value is converted to and from the database using the given conversion expressions.
public virtual PropertyBuilder<TProperty> HasConversion<TProvider>(Expression<Func<TProperty, TProvider>> convertToProviderExpression, Expression<Func<TProvider, TProperty>> convertFromProviderExpression)
Parameters
convertToProviderExpression
Expression<Func<TProperty, TProvider>>An expression to convert objects when writing data to the store.
convertFromProviderExpression
Expression<Func<TProvider, TProperty>>An expression to convert objects when reading data from the store.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
Type Parameters
TProvider
The store type generated by the conversions.
HasConversion<TProvider>(Expression<Func<TProperty, TProvider>>, Expression<Func<TProvider, TProperty>>, ValueComparer?)
Configures the property so that the property value is converted to and from the database using the given conversion expressions.
public virtual PropertyBuilder<TProperty> HasConversion<TProvider>(Expression<Func<TProperty, TProvider>> convertToProviderExpression, Expression<Func<TProvider, TProperty>> convertFromProviderExpression, ValueComparer? valueComparer)
Parameters
convertToProviderExpression
Expression<Func<TProperty, TProvider>>An expression to convert objects when writing data to the store.
convertFromProviderExpression
Expression<Func<TProvider, TProperty>>An expression to convert objects when reading data from the store.
valueComparer
ValueComparerThe comparer to use for values before conversion.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
Type Parameters
TProvider
The store type generated by the conversions.
HasConversion<TProvider>(Expression<Func<TProperty, TProvider>>, Expression<Func<TProvider, TProperty>>, ValueComparer?, ValueComparer?)
Configures the property so that the property value is converted to and from the database using the given conversion expressions.
public virtual PropertyBuilder<TProperty> HasConversion<TProvider>(Expression<Func<TProperty, TProvider>> convertToProviderExpression, Expression<Func<TProvider, TProperty>> convertFromProviderExpression, ValueComparer? valueComparer, ValueComparer? providerComparer)
Parameters
convertToProviderExpression
Expression<Func<TProperty, TProvider>>An expression to convert objects when writing data to the store.
convertFromProviderExpression
Expression<Func<TProvider, TProperty>>An expression to convert objects when reading data from the store.
valueComparer
ValueComparerThe comparer to use for values before conversion.
providerComparer
ValueComparerThe comparer to use for the provider values.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
Type Parameters
TProvider
The store type generated by the conversions.
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.
public virtual PropertyBuilder<TProperty> HasConversion<TConversion, TComparer>() where TComparer : ValueComparer
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
Type Parameters
TConversion
The type to convert to and from or a type that inherits from ValueConverter.
TComparer
A type that inherits from ValueComparer.
HasConversion<TConversion, TComparer, TProviderComparer>()
Configures the property so that the property value is converted before writing to the database and converted back when reading from the database.
public virtual PropertyBuilder<TProperty> HasConversion<TConversion, TComparer, TProviderComparer>() where TComparer : ValueComparer where TProviderComparer : ValueComparer
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
Type Parameters
TConversion
The type to convert to and from or a type that inherits from ValueConverter.
TComparer
A type that inherits from ValueComparer.
TProviderComparer
A type that inherits from ValueComparer to use for the provider values.
HasField(string)
Sets the backing field to use for this property.
public virtual PropertyBuilder<TProperty> HasField(string fieldName)
Parameters
fieldName
stringThe field name.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
Remarks
Backing fields are normally found by convention. 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).
See Backing fields for more information and examples.
HasMaxLength(int)
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).
public virtual PropertyBuilder<TProperty> HasMaxLength(int maxLength)
Parameters
maxLength
intThe maximum length of data allowed in the property. A value of
-1
indicates that the property has no maximum length.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
HasPrecision(int)
Configures the precision of the property.
public virtual PropertyBuilder<TProperty> HasPrecision(int precision)
Parameters
precision
intThe precision of the property.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
HasPrecision(int, int)
Configures the precision and scale of the property.
public virtual PropertyBuilder<TProperty> HasPrecision(int precision, int scale)
Parameters
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
HasSentinel(object?)
Configures the value that will be used to determine if the property has been set or not. If the property is set to the sentinel value, then it is considered not set. By default, the sentinel value is the CLR default value for the type of the property.
public virtual PropertyBuilder<TProperty> HasSentinel(object? sentinel)
Parameters
sentinel
objectThe sentinel value.
Returns
- PropertyBuilder<TProperty>
The same builder instance if the configuration was applied, null otherwise.
HasValueGenerator(Func<IProperty, ITypeBase, ValueGenerator>)
Configures a factory for creating a ValueGenerator to use to generate values for this property.
public virtual PropertyBuilder<TProperty> HasValueGenerator(Func<IProperty, ITypeBase, ValueGenerator> factory)
Parameters
factory
Func<IProperty, ITypeBase, ValueGenerator>A delegate that will be used to create value generator instances.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
Remarks
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.
public virtual PropertyBuilder<TProperty> HasValueGenerator(Type? valueGeneratorType)
Parameters
valueGeneratorType
TypeA type that inherits from ValueGenerator.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
Remarks
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(Type?)
Configures the ValueGeneratorFactory for creating a ValueGenerator to use to generate values for this property.
public virtual PropertyBuilder<TProperty> HasValueGeneratorFactory(Type? valueGeneratorFactoryType)
Parameters
valueGeneratorFactoryType
TypeA type that inherits from ValueGeneratorFactory.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
Remarks
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.
public virtual PropertyBuilder<TProperty> HasValueGeneratorFactory<TFactory>() where TFactory : ValueGeneratorFactory
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
Type Parameters
TFactory
A type that inherits from ValueGeneratorFactory.
Remarks
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.
public virtual PropertyBuilder<TProperty> HasValueGenerator<TGenerator>() where TGenerator : ValueGenerator
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
Type Parameters
TGenerator
A type that inherits from ValueGenerator.
Remarks
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.
IsConcurrencyToken(bool)
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.
public virtual PropertyBuilder<TProperty> IsConcurrencyToken(bool concurrencyToken = true)
Parameters
concurrencyToken
boolA value indicating whether this property is a concurrency token.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
IsRequired(bool)
Configures whether this property must have a value assigned or whether 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.
public virtual PropertyBuilder<TProperty> IsRequired(bool required = true)
Parameters
required
boolA value indicating whether the property is required.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
IsRowVersion()
Configures the property as ValueGeneratedOnAddOrUpdate() and IsConcurrencyToken(bool).
public virtual PropertyBuilder<TProperty> IsRowVersion()
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
Remarks
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(bool)
Configures the property as capable of persisting unicode characters. Can only be set on string properties.
public virtual PropertyBuilder<TProperty> IsUnicode(bool unicode = true)
Parameters
unicode
boolA value indicating whether the property can contain unicode characters.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
UsePropertyAccessMode(PropertyAccessMode)
Sets the PropertyAccessMode to use for this property.
public virtual PropertyBuilder<TProperty> UsePropertyAccessMode(PropertyAccessMode propertyAccessMode)
Parameters
propertyAccessMode
PropertyAccessModeThe PropertyAccessMode to use for this property.
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
Remarks
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 when an instance of this entity type is saved.
public virtual PropertyBuilder<TProperty> ValueGeneratedNever()
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
Remarks
Note that temporary values may still be generated for use internally before a new entity 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.
public virtual PropertyBuilder<TProperty> ValueGeneratedOnAdd()
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
ValueGeneratedOnAddOrUpdate()
Configures a property to have a value generated when saving a new or existing entity.
public virtual PropertyBuilder<TProperty> ValueGeneratedOnAddOrUpdate()
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
ValueGeneratedOnUpdate()
Configures a property to have a value generated when saving an existing entity.
public virtual PropertyBuilder<TProperty> ValueGeneratedOnUpdate()
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.
ValueGeneratedOnUpdateSometimes()
Configures a property to have a value generated under certain conditions when saving an existing entity.
public virtual PropertyBuilder<TProperty> ValueGeneratedOnUpdateSometimes()
Returns
- PropertyBuilder<TProperty>
The same builder instance so that multiple configuration calls can be chained.