Table of Contents

Class ModelBuilder

Namespace
Microsoft.EntityFrameworkCore
Assembly
Microsoft.EntityFrameworkCore.dll

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 : IInfrastructure<IConventionModelBuilder>
Inheritance
ModelBuilder
Implements
Inherited Members
Extension Methods

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.

public ModelBuilder()

Remarks

ModelBuilder(ConventionSet)

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

public ModelBuilder(ConventionSet conventions)

Parameters

conventions ConventionSet

The conventions to be applied to the model.

Remarks

ModelBuilder(ConventionSet, ModelDependencies)

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

public ModelBuilder(ConventionSet conventions, ModelDependencies modelDependencies)

Parameters

conventions ConventionSet

The conventions to be applied to the model.

modelDependencies ModelDependencies

The dependencies object for the model.

Remarks

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.

[EntityFrameworkInternal]
public ModelBuilder(ConventionSet conventions, ModelDependencies? modelDependencies, ModelConfiguration? modelConfiguration)

Parameters

conventions ConventionSet
modelDependencies ModelDependencies
modelConfiguration ModelConfiguration

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.

[EntityFrameworkInternal]
public ModelBuilder(IMutableModel model)

Parameters

model IMutableModel

Properties

Model

The model being configured.

public virtual IMutableModel Model { get; }

Property Value

IMutableModel

Remarks

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

Methods

ApplyConfiguration<TEntity>(IEntityTypeConfiguration<TEntity>)

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

public virtual ModelBuilder ApplyConfiguration<TEntity>(IEntityTypeConfiguration<TEntity> configuration) where TEntity : class

Parameters

configuration IEntityTypeConfiguration<TEntity>

The configuration to be applied.

Returns

ModelBuilder

The same ModelBuilder instance so that additional configuration calls can be chained.

Type Parameters

TEntity

The entity type to be configured.

Remarks

See Modeling entity types and relationships for more information and examples.

ApplyConfigurationsFromAssembly(Assembly, Func<Type, bool>?)

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

public virtual ModelBuilder ApplyConfigurationsFromAssembly(Assembly assembly, Func<Type, bool>? predicate = null)

Parameters

assembly Assembly

The assembly to scan.

predicate Func<Type, bool>

Optional predicate to filter types within the assembly.

Returns

ModelBuilder

The same ModelBuilder instance so that additional configuration calls can be chained.

Remarks

See Modeling entity types and relationships for more information and examples.

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.

public virtual EntityTypeBuilder Entity(string name)

Parameters

name string

The name of the entity type to be configured.

Returns

EntityTypeBuilder

An object that can be used to configure the entity type.

Remarks

See Modeling entity types for more information and examples.

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.

public virtual ModelBuilder Entity(string name, Action<EntityTypeBuilder> buildAction)

Parameters

name string

The name of the entity type to be configured.

buildAction Action<EntityTypeBuilder>

An action that performs configuration of the entity type.

Returns

ModelBuilder

The same ModelBuilder instance so that additional configuration calls can be chained.

Remarks

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.

See Modeling entity types for more information and examples.

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.

public virtual EntityTypeBuilder Entity(Type type)

Parameters

type Type

The entity type to be configured.

Returns

EntityTypeBuilder

An object that can be used to configure the entity type.

Remarks

See Modeling entity types for more information and examples.

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.

public virtual ModelBuilder Entity(Type type, Action<EntityTypeBuilder> buildAction)

Parameters

type Type

The entity type to be configured.

buildAction Action<EntityTypeBuilder>

An action that performs configuration of the entity type.

Returns

ModelBuilder

The same ModelBuilder instance so that additional configuration calls can be chained.

Remarks

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.

See Modeling entity types for more information and examples.

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.

public virtual EntityTypeBuilder<TEntity> Entity<TEntity>() where TEntity : class

Returns

EntityTypeBuilder<TEntity>

An object that can be used to configure the entity type.

Type Parameters

TEntity

The entity type to be configured.

Remarks

See Modeling entity types for more information and examples.

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.

public virtual ModelBuilder Entity<TEntity>(Action<EntityTypeBuilder<TEntity>> buildAction) where TEntity : class

Parameters

buildAction Action<EntityTypeBuilder<TEntity>>

An action that performs configuration of the entity type.

Returns

ModelBuilder

The same ModelBuilder instance so that additional configuration calls can be chained.

Type Parameters

TEntity

The entity type to be configured.

Remarks

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.

See Modeling entity types for more information and examples.

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.

public virtual IModel FinalizeModel()

Returns

IModel

The finalized model.

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.

public virtual ModelBuilder HasAnnotation(string annotation, object? value)

Parameters

annotation string

The key of the annotation to be added or updated.

value object

The value to be stored in the annotation.

Returns

ModelBuilder

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

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.

public virtual ModelBuilder HasChangeTrackingStrategy(ChangeTrackingStrategy changeTrackingStrategy)

Parameters

changeTrackingStrategy ChangeTrackingStrategy

The change tracking strategy to be used.

Returns

ModelBuilder

The same ModelBuilder instance so that additional configuration calls can be chained.

Remarks

See Change detection and notifications for more information and examples.

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.

public virtual ModelBuilder Ignore(string typeName)

Parameters

typeName string

The name of the entity type to be removed from the model.

Returns

ModelBuilder

The same ModelBuilder instance so that additional configuration calls can be chained.

Remarks

See Modeling entity types for more information and examples.

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.

public virtual ModelBuilder Ignore(Type type)

Parameters

type Type

The entity type to be removed from the model.

Returns

ModelBuilder

The same ModelBuilder instance so that additional configuration calls can be chained.

Remarks

See Modeling entity types for more information and examples.

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.

public virtual ModelBuilder Ignore<TEntity>() where TEntity : class

Returns

ModelBuilder

The same ModelBuilder instance so that additional configuration calls can be chained.

Type Parameters

TEntity

The entity type to be removed from the model.

Remarks

See Modeling entity types for more information and examples.

Owned(Type)

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

public virtual OwnedEntityTypeBuilder Owned(Type type)

Parameters

type Type

The entity type to be configured.

Returns

OwnedEntityTypeBuilder

Remarks

See Owned types in EF Core for more information and examples.

Owned<T>()

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

public virtual OwnedEntityTypeBuilder<T> Owned<T>() where T : class

Returns

OwnedEntityTypeBuilder<T>

Type Parameters

T

The entity type to be configured.

Remarks

See Owned types in EF Core for more information and examples.

SharedTypeEntity(string, Type)

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

public virtual EntityTypeBuilder SharedTypeEntity(string name, Type type)

Parameters

name string

The name of the entity type to be configured.

type Type

The CLR type of the entity type to be configured.

Returns

EntityTypeBuilder

An object that can be used to configure the entity type.

Remarks

If an entity type with the provided name is not already part of the model, a new entity type with provided CLR type will be added to the model as shared type entity type.

Shared type entity type is an entity type which can share CLR type with other types in the model but has a unique name and always identified by the name.

See Modeling entity types and Shared entity types for more information and examples.

SharedTypeEntity(string, Type, Action<EntityTypeBuilder>)

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

public virtual ModelBuilder SharedTypeEntity(string name, Type type, Action<EntityTypeBuilder> buildAction)

Parameters

name string

The name of the entity type to be configured.

type Type

The CLR type of the entity type to be configured.

buildAction Action<EntityTypeBuilder>

An action that performs configuration of the entity type.

Returns

ModelBuilder

The same ModelBuilder instance so that additional configuration calls can be chained.

Remarks

If an entity type with the provided name is not already part of the model, a new entity type with provided CLR type will be added to the model as shared type entity type.

Shared type entity type is an entity type which can share CLR type with other types in the model but has a unique name and always identified by the name.

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.

See Modeling entity types and Shared entity types for more information and examples.

SharedTypeEntity<TEntity>(string)

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

public virtual EntityTypeBuilder<TEntity> SharedTypeEntity<TEntity>(string name) where TEntity : class

Parameters

name string

The name of the entity type to be configured.

Returns

EntityTypeBuilder<TEntity>

An object that can be used to configure the entity type.

Type Parameters

TEntity

The CLR type of the entity type to be configured.

Remarks

If an entity type with the provided name is not already part of the model, a new entity type with provided CLR type will be added to the model as shared type entity type.

Shared type entity type is an entity type which can share CLR type with other types in the model but has a unique name and always identified by the name.

See Modeling entity types and Shared entity types for more information and examples.

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

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

public virtual ModelBuilder SharedTypeEntity<TEntity>(string name, Action<EntityTypeBuilder<TEntity>> buildAction) where TEntity : class

Parameters

name string

The name of the entity type to be configured.

buildAction Action<EntityTypeBuilder<TEntity>>

An action that performs configuration of the entity type.

Returns

ModelBuilder

The same ModelBuilder instance so that additional configuration calls can be chained.

Type Parameters

TEntity

The CLR type of the entity type to be configured.

Remarks

If an entity type with the provided name is not already part of the model, a new entity type with provided CLR type will be added to the model as shared type entity type.

Shared type entity type is an entity type which can share CLR type with other types in the model but has a unique name and always identified by the name.

This overload allows configuration of the entity type to be done inline 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.

See Modeling entity types and Shared entity types for more information and examples.

UsePropertyAccessMode(PropertyAccessMode)

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

public virtual ModelBuilder UsePropertyAccessMode(PropertyAccessMode propertyAccessMode)

Parameters

propertyAccessMode PropertyAccessMode

The PropertyAccessMode to use for properties of this model.

Returns

ModelBuilder

The same ModelBuilder instance so that additional 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 all properties in the model as described in the PropertyAccessMode enum.

See Property versus field access in EF Core for more information and examples.