ModelBuilder ModelBuilder 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
ModelBuilderModelBuilderModelBuilder
Implements

Constructors

ModelBuilder(ConventionSet) ModelBuilder(ConventionSet) ModelBuilder(ConventionSet)

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

Properties

Model Model Model

The model being configured.

Methods

ApplyConfiguration<TEntity>(IEntityTypeConfiguration<TEntity>) ApplyConfiguration<TEntity>(IEntityTypeConfiguration<TEntity>) ApplyConfiguration<TEntity>(IEntityTypeConfiguration<TEntity>)

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

ApplyConfiguration<TQuery>(IQueryTypeConfiguration<TQuery>) ApplyConfiguration<TQuery>(IQueryTypeConfiguration<TQuery>) ApplyConfiguration<TQuery>(IQueryTypeConfiguration<TQuery>)

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

Entity(String) Entity(String) 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) 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(String, Action<EntityTypeBuilder>) Entity(String, Action<EntityTypeBuilder>) 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, Action<EntityTypeBuilder>) Entity(Type, Action<EntityTypeBuilder>) 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>() Entity<TEntity>() 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>>) Entity<TEntity>(Action<EntityTypeBuilder<TEntity>>) 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) Equals(Object) Equals(Object)

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

GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

HasAnnotation(String, Object) HasAnnotation(String, Object) 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) HasChangeTrackingStrategy(ChangeTrackingStrategy) 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) HasChangeTrackingStrategy(ChangeTrackingStrategy) 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) Ignore(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>() Ignore<TEntity>() 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) Owned(Type) Owned(Type)

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

Owned<T>() Owned<T>() Owned<T>()

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

Query(Type) Query(Type) 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<QueryTypeBuilder>) Query(Type, Action<QueryTypeBuilder>) 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>() Query<TQuery>() 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>>) Query<TQuery>(Action<QueryTypeBuilder<TQuery>>) 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() ToString() ToString()

Returns a string that represents the current object.

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

GetInfrastructure<T>(IInfrastructure<T>) GetInfrastructure<T>(IInfrastructure<T>) 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) HasDbFunction(ModelBuilder, MethodInfo) HasDbFunction(ModelBuilder, MethodInfo)

Configures a database function when targeting a relational database.

HasDbFunction(ModelBuilder, MethodInfo, Action<DbFunctionBuilder>) HasDbFunction(ModelBuilder, MethodInfo, Action<DbFunctionBuilder>) HasDbFunction(ModelBuilder, MethodInfo, Action<DbFunctionBuilder>)

Configures a database function when targeting a relational database.

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

Configures a database function when targeting a relational database.

HasDefaultSchema(ModelBuilder, String) HasDefaultSchema(ModelBuilder, String) HasDefaultSchema(ModelBuilder, String)

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

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

Configures a database sequence when targeting a relational database.

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

Configures a database sequence when targeting a relational database.

HasSequence(ModelBuilder, String, String) HasSequence(ModelBuilder, String, String) HasSequence(ModelBuilder, String, String)

Configures a database sequence when targeting a relational database.

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

Configures a database sequence when targeting a relational database.

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

Configures a database sequence when targeting a relational database.

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

Configures a database sequence when targeting a relational database.

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

Configures a database sequence when targeting a relational database.

HasSequence(ModelBuilder, Type, String, String) HasSequence(ModelBuilder, Type, String, String) HasSequence(ModelBuilder, Type, String, String)

Configures a database sequence when targeting a relational database.

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

Configures a database sequence when targeting a relational database.

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

Configures a database sequence when targeting a relational database.

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

Configures a database sequence when targeting a relational database.

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

Configures a database sequence when targeting a relational database.

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

Configures a database sequence when targeting a relational database.

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

Configures a database sequence when targeting a relational database.

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

Configures a database sequence when targeting a relational database.

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

Configures a database sequence when targeting SQL Server.

ForSqlServerHasSequence(ModelBuilder, String, String) ForSqlServerHasSequence(ModelBuilder, String, String) ForSqlServerHasSequence(ModelBuilder, String, String)

Configures a database sequence when targeting SQL Server.

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

Configures a database sequence when targeting SQL Server.

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

Configures a database sequence when targeting SQL Server.

ForSqlServerHasSequence(ModelBuilder, Type, String, String) ForSqlServerHasSequence(ModelBuilder, Type, String, String) ForSqlServerHasSequence(ModelBuilder, Type, String, String)

Configures a database sequence when targeting SQL Server.

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

Configures a database sequence when targeting SQL Server.

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

Configures a database sequence when targeting SQL Server.

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

Configures a database sequence when targeting SQL Server.

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

Configures a database sequence when targeting SQL Server.

ForSqlServerUseIdentityColumns(ModelBuilder) ForSqlServerUseIdentityColumns(ModelBuilder) 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) ForSqlServerUseSequenceHiLo(ModelBuilder, String, String) 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.

Applies to