PropertyBuilder<TProperty> PropertyBuilder(Of TProperty) Class

Definition

Provides a simple API for configuring a Property.

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<TProperty> : PropertyBuilder, IInfrastructure<IMutableModel>, IInfrastructure<InternalPropertyBuilder>
Public Class PropertyBuilder(Of TProperty)
    Inherits PropertyBuilder
    Implements IInfrastructure(Of IMutableModel), IInfrastructure(Of InternalPropertyBuilder)
Type Parameters
TProperty
Inheritance
System.ObjectSystem.Object
PropertyBuilder<TProperty>PropertyBuilder(Of TProperty)
Implements

Constructors

PropertyBuilder(InternalPropertyBuilder) 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.

public PropertyBuilder(InternalPropertyBuilder builder)
Public Sub New(builder As InternalPropertyBuilder)
Parameters

Methods

Equals(Object) Equals(Object)

Inherited from System.Object

Equals(Object, Object) Equals(Object, Object)

Inherited from System.Object

GetHashCode() GetHashCode()

Inherited from System.Object

GetType() GetType()

Inherited from System.Object

HasAnnotation(String, Object) HasAnnotation(String, Object)

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

public virtual PropertyBuilder<TProperty> HasAnnotation(string annotation, object value)
Public Overridable Function HasAnnotation(annotation As String, value As Object) As PropertyBuilder(Of TProperty)
Parameters
annotation
System.String System.String

The key of the annotation to be added or updated.

value
System.Object System.Object

The value to be stored in the annotation.

Returns
PropertyBuilder<TProperty> PropertyBuilder(Of TProperty)

The same builder instance so that multiple configuration calls can be chained.

HasField(String) 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).

public virtual PropertyBuilder<TProperty> HasField(string fieldName)
Public Overridable Function HasField(fieldName As String) As PropertyBuilder(Of TProperty)
Parameters
fieldName
System.String System.String

The field name.

Returns
PropertyBuilder<TProperty> PropertyBuilder(Of TProperty)

The same builder instance so that multiple configuration calls can be chained.

HasMaxLength(Int32) 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 System.String properties).

public virtual PropertyBuilder<TProperty> HasMaxLength(int maxLength)
Public Overridable Function HasMaxLength(maxLength As Integer) As PropertyBuilder(Of TProperty)
Parameters
maxLength
System.Int32 System.Int32

The maximum length of data allowed in the property.

Returns
PropertyBuilder<TProperty> PropertyBuilder(Of TProperty)

The same builder instance so that multiple configuration calls can be chained.

HasValueGenerator(Func) HasValueGenerator(Func(Of 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.

public virtual PropertyBuilder<TProperty> HasValueGenerator(Func<IProperty, IEntityType, ValueGenerator> factory)
Public Overridable Function HasValueGenerator(factory As Func(Of IProperty, IEntityType, ValueGenerator)) As PropertyBuilder(Of TProperty)
Parameters
factory
System.Func<IProperty, IEntityType, ValueGenerator> System.Func(Of IProperty, IEntityType, ValueGenerator)

A delegate that will be used to create value generator instances.

Returns
PropertyBuilder<TProperty> PropertyBuilder(Of TProperty)

The same builder instance so that multiple configuration calls can be chained.

HasValueGenerator(Type) 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.

public virtual PropertyBuilder<TProperty> HasValueGenerator(Type valueGeneratorType)
Public Overridable Function HasValueGenerator(valueGeneratorType As Type) As PropertyBuilder(Of TProperty)
Parameters
valueGeneratorType
System.Type System.Type

A type that inherits from ValueGenerator

Returns
PropertyBuilder<TProperty> PropertyBuilder(Of TProperty)

The same builder instance so that multiple configuration calls can be chained.

HasValueGenerator() HasValueGenerator(Of 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.

public virtual PropertyBuilder<TProperty> HasValueGenerator<TGenerator>()where TGenerator : ValueGenerator
Public Overridable Function HasValueGenerator(Of TGenerator As ValueGenerator) As PropertyBuilder(Of TProperty)
Type Parameters
TGenerator
Returns
PropertyBuilder<TProperty> PropertyBuilder(Of TProperty)

The same builder instance so that multiple configuration calls can be chained.

IsConcurrencyToken(Boolean) 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.

public virtual PropertyBuilder<TProperty> IsConcurrencyToken(bool concurrencyToken = true)
Public Overridable Function IsConcurrencyToken(concurrencyToken As Boolean = True) As PropertyBuilder(Of TProperty)
Parameters
concurrencyToken
System.Boolean System.Boolean

A value indicating whether this property is a concurrency token.

Returns
PropertyBuilder<TProperty> PropertyBuilder(Of TProperty)

The same builder instance so that multiple configuration calls can be chained.

IsRequired(Boolean) IsRequired(Boolean)

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)
Public Overridable Function IsRequired(required As Boolean = True) As PropertyBuilder(Of TProperty)
Parameters
required
System.Boolean System.Boolean

A value indicating whether the property is required.

Returns
PropertyBuilder<TProperty> PropertyBuilder(Of TProperty)

The same builder instance so that multiple configuration calls can be chained.

IsRowVersion() 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.

public virtual PropertyBuilder<TProperty> IsRowVersion()
Public Overridable Function IsRowVersion As PropertyBuilder(Of TProperty)
Returns
PropertyBuilder<TProperty> PropertyBuilder(Of TProperty)

The same builder instance so that multiple configuration calls can be chained.

IsUnicode(Boolean) IsUnicode(Boolean)

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

public virtual PropertyBuilder<TProperty> IsUnicode(bool unicode = true)
Public Overridable Function IsUnicode(unicode As Boolean = True) As PropertyBuilder(Of TProperty)
Parameters
unicode
System.Boolean System.Boolean

A value indicating whether the property can contain unicode characters or not.

Returns
PropertyBuilder<TProperty> PropertyBuilder(Of TProperty)

The same builder instance so that multiple configuration calls can be chained.

MemberwiseClone() MemberwiseClone()

Inherited from System.Object

ReferenceEquals(Object, Object) ReferenceEquals(Object, Object)

Inherited from System.Object

ToString() ToString()

Inherited from System.Object

UsePropertyAccessMode(PropertyAccessMode) 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.

public virtual PropertyBuilder<TProperty> UsePropertyAccessMode(PropertyAccessMode propertyAccessMode)
Public Overridable Function UsePropertyAccessMode(propertyAccessMode As PropertyAccessMode) As PropertyBuilder(Of TProperty)
Parameters
propertyAccessMode
PropertyAccessMode PropertyAccessMode

The PropertyAccessMode to use for this property.

Returns
PropertyBuilder<TProperty> PropertyBuilder(Of TProperty)

The same builder instance so that multiple configuration calls can be chained.

ValueGeneratedNever() ValueGeneratedNever()

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

public virtual PropertyBuilder<TProperty> ValueGeneratedNever()
Public Overridable Function ValueGeneratedNever As PropertyBuilder(Of TProperty)
Returns
PropertyBuilder<TProperty> PropertyBuilder(Of 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() 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()
Public Overridable Function ValueGeneratedOnAdd As PropertyBuilder(Of TProperty)
Returns
PropertyBuilder<TProperty> PropertyBuilder(Of TProperty)

The same builder instance so that multiple configuration calls can be chained.

ValueGeneratedOnAddOrUpdate() ValueGeneratedOnAddOrUpdate()

Configures a property to have a value generated only when saving a new or existing entity, unless a non-null, non-temporary value has been set for a new entity, or the existing property value has been modified for an existing entity, in which case the set value will be saved instead.

public virtual PropertyBuilder<TProperty> ValueGeneratedOnAddOrUpdate()
Public Overridable Function ValueGeneratedOnAddOrUpdate As PropertyBuilder(Of TProperty)
Returns
PropertyBuilder<TProperty> PropertyBuilder(Of TProperty)

The same builder instance so that multiple configuration calls can be chained.

Properties

IInfrastructure.Instance Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure.Instance

The model that the property belongs to.

(Inherited from PropertyBuilder)

IInfrastructure.Instance Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure.Instance

The internal builder being used to configure the property.

(Inherited from PropertyBuilder)

Metadata Metadata

The property being configured.

(Inherited from PropertyBuilder)