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 : IInfrastructure<InternalModelBuilder>
Public Class ModelBuilder
    Implements IInfrastructure(Of InternalModelBuilder)
Inheritance
System.ObjectSystem.Object
ModelBuilderModelBuilder
Implements

Constructors

ModelBuilder(ConventionSet) ModelBuilder(ConventionSet)

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

public ModelBuilder(ConventionSet conventions)
Public Sub New(conventions As ConventionSet)
Parameters
conventions
ConventionSet ConventionSet

The conventions to be applied to the model.

Properties

Model Model

The model being configured.

public virtual IMutableModel Model { get; }
Public Overridable ReadOnly Property Model As IMutableModel

Methods

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.

public virtual EntityTypeBuilder Entity(string name)
Public Overridable Function Entity(name As String) As EntityTypeBuilder
Parameters
name
System.String System.String

The name of the entity type to be configured.

Returns

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

Entity(String, Action) Entity(String, Action(Of 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.

public virtual ModelBuilder Entity(string name, Action<EntityTypeBuilder> buildAction)
Public Overridable Function Entity(name As String, buildAction As Action(Of EntityTypeBuilder)) As ModelBuilder
Parameters
name
System.String System.String

The name of the entity type to be configured.

buildAction
System.Action<EntityTypeBuilder> System.Action(Of EntityTypeBuilder)

An action that performs configuration of the entity type.

Returns

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

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.

public virtual EntityTypeBuilder Entity(Type type)
Public Overridable Function Entity(type As Type) As EntityTypeBuilder
Parameters
type
System.Type System.Type

The entity type to be configured.

Returns

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

Entity(Type, Action) Entity(Type, Action(Of 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.

public virtual ModelBuilder Entity(Type type, Action<EntityTypeBuilder> buildAction)
Public Overridable Function Entity(type As Type, buildAction As Action(Of EntityTypeBuilder)) As ModelBuilder
Parameters
type
System.Type System.Type

The entity type to be configured.

buildAction
System.Action<EntityTypeBuilder> System.Action(Of EntityTypeBuilder)

An action that performs configuration of the entity type.

Returns

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

Entity() Entity(Of 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
Public Overridable Function Entity(Of TEntity As Class) As EntityTypeBuilder(Of TEntity)
Type Parameters
TEntity

The entity type to be configured.

Returns
EntityTypeBuilder<TEntity> EntityTypeBuilder(Of TEntity)

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

Entity(Action<>>) Entity(Of TEntity)(Action(Of EntityTypeBuilder(Of 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.

public virtual ModelBuilder Entity<TEntity>(Action<EntityTypeBuilder<TEntity>> buildAction)where TEntity : class
Public Overridable Function Entity(Of TEntity As Class)(buildAction As Action(Of EntityTypeBuilder(Of TEntity))) As ModelBuilder
Type Parameters
TEntity

The entity type to be configured.

Parameters
buildAction
System.Action<EntityTypeBuilder<TEntity>> System.Action(Of EntityTypeBuilder(Of TEntity))

An action that performs configuration of the entity type.

Returns

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

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 model. If an annotation with the key specified in annotation already exists it's value will be updated.

public virtual ModelBuilder HasAnnotation(string annotation, object value)
Public Overridable Function HasAnnotation(annotation As String, value As Object) As ModelBuilder
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

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

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.

public virtual ModelBuilder HasChangeTrackingStrategy(ChangeTrackingStrategy changeTrackingStrategy)
Public Overridable Function HasChangeTrackingStrategy(changeTrackingStrategy As ChangeTrackingStrategy) As ModelBuilder
Parameters
changeTrackingStrategy
ChangeTrackingStrategy ChangeTrackingStrategy

The change tracking strategy to be used.

Returns

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

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.

public virtual ModelBuilder Ignore(Type type)
Public Overridable Function Ignore(type As Type) As ModelBuilder
Parameters
type
System.Type System.Type

The entity type to be removed from the model.

Returns

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

Ignore() Ignore(Of 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
Public Overridable Function Ignore(Of TEntity As Class) As ModelBuilder
Type Parameters
TEntity

The entity type to be removed from the model.

Returns

The same ModelBuilder instance so that additional 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 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.

public virtual ModelBuilder UsePropertyAccessMode(PropertyAccessMode propertyAccessMode)
Public Overridable Function UsePropertyAccessMode(propertyAccessMode As PropertyAccessMode) As ModelBuilder
Parameters
propertyAccessMode
PropertyAccessMode PropertyAccessMode

The PropertyAccessMode to use for properties of this model.

Returns

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

Explicit Interface Implementations

IInfrastructure.Instance Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure.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.

InternalModelBuilder IInfrastructure<InternalModelBuilder>.Instance { get; }
ReadOnly WriteOnly Property Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<Microsoft.EntityFrameworkCore.Metadata.Internal.InternalModelBuilder>.Instance As InternalModelBuilder Implements IInfrastructure(Of InternalModelBuilder).Instance
Returns