ModelBuilder ModelBuilder Class

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.

Syntax

Declaration

public class ModelBuilder : IInfrastructure<InternalModelBuilder>Public Class ModelBuilder
    Implements IInfrastructure(Of InternalModelBuilder)

Inheritance Hierarchy

  • System.Object
    System.Object
  • ModelBuilder
    ModelBuilder

Inherited Members

ToString(),
ToString(),
Equals(Object),
Equals(Object),
Equals(Object, Object),
Equals(Object, Object),
ReferenceEquals(Object, Object),
ReferenceEquals(Object, Object),
GetHashCode(),
GetHashCode(),
GetType(),
GetType(),
MemberwiseClone()
MemberwiseClone()

Constructors summary

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

Properties summary

The model being configured.

Methods summary

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.

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.

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.

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.

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.

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.

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.

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.

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

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

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.

Explicit Interface Implementations summary

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.

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

Properties

  • Model
    Model

    The model being configured.

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

    Property Value

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

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

      An action that performs configuration of the entity type.

    Returns

  • 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

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

      An action that performs configuration of the entity type.

    Returns

  • Entity<TEntity>()
    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 : classPublic Overridable Function Entity(Of TEntity As Class) As EntityTypeBuilder(Of TEntity)

    Returns

  • Entity<TEntity>(Action<EntityTypeBuilder<TEntity>>)
    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 : classPublic Overridable Function Entity(Of TEntity As Class)(buildAction As Action(Of EntityTypeBuilder(Of TEntity))) As ModelBuilder

    Parameters

    Returns

  • 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

  • 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

    Returns

  • 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

  • Ignore<TEntity>()
    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 : classPublic Overridable Function Ignore(Of TEntity As Class) As ModelBuilder

    Returns

  • 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

    Returns

Explicit Interface Implementations

  • IInfrastructure<InternalModelBuilder>.Instance
    Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<Microsoft.EntityFrameworkCore.Metadata.Internal.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.

    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

Details

Assembly

Microsoft.EntityFrameworkCore.dll