ModelBuilder Class

Definition

Provides a simple API surface for configuring a IMutableModel that defines the shape of your entities, the relationships between them, and how they map to the database.

You can use ModelBuilder to construct a model for a context by overriding OnModelCreating(ModelBuilder) on your derived context. Alternatively you can create the model externally and set it on a DbContextOptions instance that is passed to the context constructor.

public class ModelBuilder : Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<Microsoft.EntityFrameworkCore.Metadata.Internal.InternalModelBuilder>
type ModelBuilder = class
    interface IInfrastructure<InternalModelBuilder>
Public Class ModelBuilder
Implements IInfrastructure(Of InternalModelBuilder)
Inheritance
ModelBuilder
Implements
IInfrastructure<Microsoft.EntityFrameworkCore.Metadata.Internal.InternalModelBuilder>

Constructors

ModelBuilder(ConventionSet)

Initializes a new instance of the ModelBuilder class that will apply a set of conventions.

ModelBuilder(IMutableModel)

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.

Properties

Model

The model being configured.

Methods

ApplyConfiguration<TEntity>(IEntityTypeConfiguration<TEntity>)

Applies configuration that is defined in an IEntityTypeConfiguration<TEntity> instance.

ApplyConfiguration<TQuery>(IQueryTypeConfiguration<TQuery>)

Applies configuration that is defined in an IQueryTypeConfiguration<TQuery> instance.

ApplyConfigurationsFromAssembly(Assembly, Func<Type,Boolean>)

Applies configuration from all IEntityTypeConfiguration<TEntity> and IQueryTypeConfiguration<TQuery> instances that are defined in provided assembly.

Entity(String)

Returns an object that can be used to configure a given entity type in the model. If an entity type with the provided name is not already part of the model, a new entity type that does not have a corresponding CLR type will be added to the model.

Entity(String, Action<EntityTypeBuilder>)

Performs configuration of a given entity type in the model. If an entity type with the provided name is not already part of the model, a new entity type that does not have a corresponding CLR type will be added to the model.

This overload allows configuration of the entity type to be done in line in the method call rather than being chained after a call to Entity(String). This allows additional configuration at the model level to be chained after configuration for the entity type.

Entity(Type)

Returns an object that can be used to configure a given entity type in the model. If the entity type is not already part of the model, it will be added to the model.

Entity(Type, Action<EntityTypeBuilder>)

Performs configuration of a given entity type in the model. If the entity type is not already part of the model, it will be added to the model.

This overload allows configuration of the entity type to be done in line in the method call rather than being chained after a call to Entity<TEntity>(). This allows additional configuration at the model level to be chained after configuration for the entity type.

Entity<TEntity>()

Returns an object that can be used to configure a given entity type in the model. If the entity type is not already part of the model, it will be added to the model.

Entity<TEntity>(Action<EntityTypeBuilder<TEntity>>)

Performs configuration of a given entity type in the model. If the entity type is not already part of the model, it will be added to the model.

This overload allows configuration of the entity type to be done in line in the method call rather than being chained after a call to Entity<TEntity>(). This allows additional configuration at the model level to be chained after configuration for the entity type.

Equals(Object)

Determines whether the specified object is equal to the current object.

FinalizeModel()

Forces post-processing on the model such that it is ready for use by the runtime. This post processing happens automatically when using OnModelCreating(ModelBuilder); this method allows it to be run explicitly in cases where the automatic execution is not possible.

GetHashCode()

Serves as the default hash function.

HasAnnotation(String, Object)

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

HasChangeTrackingStrategy(ChangeTrackingStrategy)

Configures the default ChangeTrackingStrategy to be used for this model. This strategy indicates how the context detects changes to properties for an instance of an entity type.

HasChangeTrackingStrategy(ChangeTrackingStrategy)

Configures the default ChangeTrackingStrategy to be used for this model. This strategy indicates how the context detects changes to properties for an instance of an entity type.

Ignore(Type)

Excludes the given entity type from the model. This method is typically used to remove types from the model that were added by convention.

Ignore<TEntity>()

Excludes the given entity type from the model. This method is typically used to remove types from the model that were added by convention.

Owned(Type)

Marks an entity type as owned. All references to this type will be configured as separate owned type instances.

Owned<T>()

Marks an entity type as owned. All references to this type will be configured as separate owned type instances.

Query(Type)

Returns an object that can be used to configure a given query type in the model. If the query type is not already part of the model, it will be added to the model.

Query(Type, Action<EntityTypeBuilder>)

Performs configuration of a given query type in the model. If the query type is not already part of the model, it will be added to the model.

This overload allows configuration of the query type to be done in line in the method call rather than being chained after a call to Query<TQuery>(). This allows additional configuration at the model level to be chained after configuration for the query type.

Query(Type, Action<QueryTypeBuilder>)

Performs configuration of a given query type in the model. If the query type is not already part of the model, it will be added to the model.

This overload allows configuration of the query type to be done in line in the method call rather than being chained after a call to Query<TQuery>(). This allows additional configuration at the model level to be chained after configuration for the query type.

Query<TQuery>()

Returns an object that can be used to configure a given query type in the model. If the query type is not already part of the model, it will be added to the model.

Query<TQuery>(Action<QueryTypeBuilder<TQuery>>)

Performs configuration of a given query type in the model. If the query type is not already part of the model, it will be added to the model.

This overload allows configuration of the query type to be done in line in the method call rather than being chained after a call to Query<TQuery>(). This allows additional configuration at the model level to be chained after configuration for the query type.

ToString()

Returns a string that represents the current object.

UsePropertyAccessMode(PropertyAccessMode)

Sets the PropertyAccessMode to use for all properties of this entity type.

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 all properties in the model as described in the PropertyAccessMode enum.

UsePropertyAccessMode(PropertyAccessMode)

Sets the PropertyAccessMode to use for all properties of this entity type.

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 all properties in the model as described in the PropertyAccessMode enum.

Explicit Interface Implementations

IInfrastructure<InternalModelBuilder>.Instance

The internal ModelBuilder being used to configure this model.

This property is intended for use by extension methods to configure the model. It is not intended to be used in application code.

Extension Methods

HasDefaultContainer(ModelBuilder, String)

Configures the default container name that will be used if no name is explicitly configured for an entity type.

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.

HasDbFunction(ModelBuilder, MethodInfo)

Configures a database function when targeting a relational database.

HasDbFunction(ModelBuilder, MethodInfo, Action<DbFunctionBuilder>)

Configures a database function when targeting a relational database.

HasDbFunction<TResult>(ModelBuilder, Expression<Func<TResult>>)

Configures a database function when targeting a relational database.

HasDefaultSchema(ModelBuilder, String)

Configures the default schema that database objects should be created in, if no schema is explicitly configured.

HasSequence(ModelBuilder, String, Action<SequenceBuilder>)

Configures a database sequence when targeting a relational database.

HasSequence(ModelBuilder, String, Action<RelationalSequenceBuilder>)

Configures a database sequence when targeting a relational database.

HasSequence(ModelBuilder, String, Action<SequenceBuilder>)

Configures a database sequence when targeting a relational database.

HasSequence(ModelBuilder, String, String)

Configures a database sequence when targeting a relational database.

HasSequence(ModelBuilder, String, String, Action<SequenceBuilder>)

Configures a database sequence when targeting a relational database.

HasSequence(ModelBuilder, String, String, Action<RelationalSequenceBuilder>)

Configures a database sequence when targeting a relational database.

HasSequence(ModelBuilder, String, String, Action<SequenceBuilder>)

Configures a database sequence when targeting a relational database.

HasSequence(ModelBuilder, Type, String, Action<SequenceBuilder>)

Configures a database sequence when targeting a relational database.

HasSequence(ModelBuilder, Type, String, Action<RelationalSequenceBuilder>)

Configures a database sequence when targeting a relational database.

HasSequence(ModelBuilder, Type, String, Action<SequenceBuilder>)

Configures a database sequence when targeting a relational database.

HasSequence(ModelBuilder, Type, String, String)

Configures a database sequence when targeting a relational database.

HasSequence(ModelBuilder, Type, String, String, Action<SequenceBuilder>)

Configures a database sequence when targeting a relational database.

HasSequence(ModelBuilder, Type, String, String, Action<RelationalSequenceBuilder>)

Configures a database sequence when targeting a relational database.

HasSequence(ModelBuilder, Type, String, String, Action<SequenceBuilder>)

Configures a database sequence when targeting a relational database.

HasSequence<T>(ModelBuilder, String, Action<SequenceBuilder>)

Configures a database sequence when targeting a relational database.

HasSequence<T>(ModelBuilder, String, Action<RelationalSequenceBuilder>)

Configures a database sequence when targeting a relational database.

HasSequence<T>(ModelBuilder, String, Action<SequenceBuilder>)

Configures a database sequence when targeting a relational database.

HasSequence<T>(ModelBuilder, String, String)

Configures a database sequence when targeting a relational database.

HasSequence<T>(ModelBuilder, String, String, Action<SequenceBuilder>)

Configures a database sequence when targeting a relational database.

HasSequence<T>(ModelBuilder, String, String, Action<RelationalSequenceBuilder>)

Configures a database sequence when targeting a relational database.

HasSequence<T>(ModelBuilder, String, String, Action<SequenceBuilder>)

Configures a database sequence when targeting a relational database.

ForSqlServerHasSequence(ModelBuilder, String, Action<RelationalSequenceBuilder>)

Configures a database sequence when targeting SQL Server.

ForSqlServerHasSequence(ModelBuilder, String, String)

Configures a database sequence when targeting SQL Server.

ForSqlServerHasSequence(ModelBuilder, String, String, Action<RelationalSequenceBuilder>)

Configures a database sequence when targeting SQL Server.

ForSqlServerHasSequence(ModelBuilder, Type, String, Action<RelationalSequenceBuilder>)

Configures a database sequence when targeting SQL Server.

ForSqlServerHasSequence(ModelBuilder, Type, String, String)

Configures a database sequence when targeting SQL Server.

ForSqlServerHasSequence(ModelBuilder, Type, String, String, Action<RelationalSequenceBuilder>)

Configures a database sequence when targeting SQL Server.

ForSqlServerHasSequence<T>(ModelBuilder, String, Action<RelationalSequenceBuilder>)

Configures a database sequence when targeting SQL Server.

ForSqlServerHasSequence<T>(ModelBuilder, String, String)

Configures a database sequence when targeting SQL Server.

ForSqlServerHasSequence<T>(ModelBuilder, String, String, Action<RelationalSequenceBuilder>)

Configures a database sequence when targeting SQL Server.

ForSqlServerUseIdentityColumns(ModelBuilder)

Configures the model to use the SQL Server IDENTITY feature to generate values for key properties marked as OnAdd, when targeting SQL Server. This is the default behavior when targeting SQL Server.

ForSqlServerUseIdentityColumns(ModelBuilder, Int32, Int32)

Configures the model to use the SQL Server IDENTITY feature to generate values for key properties marked as OnAdd, when targeting SQL Server. This is the default behavior when targeting SQL Server.

ForSqlServerUseSequenceHiLo(ModelBuilder, String, String)

Configures the model to use a sequence-based hi-lo pattern to generate values for key properties marked as OnAdd, when targeting SQL Server.

HasDatabaseMaxSize(ModelBuilder, String)

Configures the maximum size for Azure SQL Database.

Units must be included, e.g. "100 MB". See Azure SQL Database documentation for all supported values.

HasPerformanceLevel(ModelBuilder, String)

Configures the performance level (SERVICE_OBJECTIVE) for Azure SQL Database as a string literal.

See Azure SQL Database documentation for supported values.

HasPerformanceLevelSql(ModelBuilder, String)

Configures the performance level (SERVICE_OBJECTIVE) for Azure SQL Database as a SQL expression.

See Azure SQL Database documentation for supported values.

HasServiceTier(ModelBuilder, String)

Configures the service tier (EDITION) for Azure SQL Database as a string literal.

See Azure SQL Database documentation for supported values.

HasServiceTierSql(ModelBuilder, String)

Configures the service tier (EDITION) for Azure SQL Database as a SQL expression.

See Azure SQL Database documentation for supported values.

UseHiLo(ModelBuilder, String, String)

Configures the model to use a sequence-based hi-lo pattern to generate values for key properties marked as OnAdd, when targeting SQL Server.

UseIdentityColumns(ModelBuilder, Int32, Int32)

Configures the model to use the SQL Server IDENTITY feature to generate values for key properties marked as OnAdd, when targeting SQL Server. This is the default behavior when targeting SQL Server.

Applies to