Sdílet prostřednictvím


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.

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

Remarks

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.

See Modeling entity types and relationships in EF Core for more information and examples.

Constructors

ModelBuilder()

Initializes a new instance of the ModelBuilder class with no conventions.

Warning: conventions are needed to build a correct model.

ModelBuilder(ConventionSet, ModelDependencies, ModelConfiguration)

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.

ModelBuilder(ConventionSet, ModelDependencies)

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

ModelBuilder(ConventionSet)

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

ModelBuilder(IMutableModel)
Obsolete.

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>)
Obsolete.

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

ApplyConfigurationsFromAssembly(Assembly, Func<Type,Boolean>)

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

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.

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

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<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.

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.

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.

HasEmbeddedDiscriminatorName(String)

Sets the name to use for discriminator properties embedded in JSON documents. The default is "$type".

Ignore(String)

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

Ignore(Type)

Excludes an entity type with given CLR 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, Action<EntityTypeBuilder>)
Obsolete.

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

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

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>>)
Obsolete.

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.

SharedTypeEntity(String, Type, Action<EntityTypeBuilder>)

Returns an object that can be used to configure a given shared type entity type in the model.

SharedTypeEntity(String, Type)

Returns an object that can be used to configure a given shared type entity type in the model.

SharedTypeEntity<TEntity>(String, Action<EntityTypeBuilder<TEntity>>)

Returns an object that can be used to configure a given shared type entity type in the model.

SharedTypeEntity<TEntity>(String)

Returns an object that can be used to configure a given shared type entity type in the model.

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.

Explicit Interface Implementations

IInfrastructure<IConventionModelBuilder>.Instance

The internal ModelBuilder being used to configure this model.

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

HasAutoscaleThroughput(ModelBuilder, Nullable<Int32>)

Configures the autoscale provisioned throughput offering.

HasDefaultContainer(ModelBuilder, String)

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

HasDiscriminatorInJsonIds(ModelBuilder, Nullable<Boolean>)

Includes the discriminator value of the entity type in the JSON "id" value. This was the default behavior before EF Core 9.

HasManualThroughput(ModelBuilder, Nullable<Int32>)

Configures the manual provisioned throughput offering.

HasRootDiscriminatorInJsonId(ModelBuilder, Nullable<Boolean>)

Includes the discriminator value of the root entity type in the JSON "id" value. This allows types with the same primary key to be saved in the same container, while still allowing "ReadItem" to be used for lookups of an unknown type.

HasShadowIds(ModelBuilder, Nullable<Boolean>)

Forces model building to always create a "__id" shadow property mapped to the JSON "id". This was the default behavior before EF Core 9.0.

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.

HasDbFunction(ModelBuilder, MethodInfo, Action<DbFunctionBuilder>)

Configures a database function when targeting a relational database.

HasDbFunction(ModelBuilder, MethodInfo)

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, 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, String, String)

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, 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(ModelBuilder, Type, String, String)

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, 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.

HasSequence<T>(ModelBuilder, String, String)

Configures a database sequence when targeting a relational database.

UseCollation(ModelBuilder, String)

Configures the database collation, which will be used by all columns without an explicit collation.

ForSqlServerHasSequence(ModelBuilder, String, Action<RelationalSequenceBuilder>)

Configures a database sequence when targeting SQL Server.

ForSqlServerHasSequence(ModelBuilder, String, 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, Type, String, Action<RelationalSequenceBuilder>)

Configures a database sequence when targeting SQL Server.

ForSqlServerHasSequence(ModelBuilder, Type, String, 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<T>(ModelBuilder, String, Action<RelationalSequenceBuilder>)

Configures a database sequence when targeting SQL Server.

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

Configures a database sequence when targeting SQL Server.

ForSqlServerHasSequence<T>(ModelBuilder, String, String)

Configures a database sequence when targeting SQL Server.

ForSqlServerUseIdentityColumns(ModelBuilder, Int32, Int32)
Obsolete.

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)

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)
Obsolete.

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.

HasPerformanceLevel(ModelBuilder, String)

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

HasPerformanceLevelSql(ModelBuilder, String)

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

HasServiceTier(ModelBuilder, String)

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

HasServiceTierSql(ModelBuilder, String)

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

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.

UseIdentityColumns(ModelBuilder, Int64, 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.

UseKeySequences(ModelBuilder, String, String)

Configures the model to use a sequence per hierarchy to generate values for key properties marked as OnAdd, when targeting SQL Server.

Applies to