Table of Contents

Interface IConventionModel

Namespace
Microsoft.EntityFrameworkCore.Metadata
Assembly
Microsoft.EntityFrameworkCore.dll

Metadata about the shape of entities, the relationships between them, and how they map to the database. A model is typically created by overriding the OnModelCreating(ModelBuilder) method on a derived DbContext.

public interface IConventionModel : IReadOnlyModel, IConventionAnnotatable, IReadOnlyAnnotatable
Inherited Members

Remarks

This interface is used during model creation and allows the metadata to be modified. Once the model is built, IModel represents a read-only view of the same metadata.

See Model building conventions for more information and examples.

Properties

Builder

Gets the builder that can be used to configure this model.

IConventionModelBuilder Builder { get; }

Property Value

IConventionModelBuilder

Methods

AddEntityType(string, bool)

Adds a state entity type of default type to the model.

IConventionEntityType? AddEntityType(string name, bool fromDataAnnotation = false)

Parameters

name string

The name of the entity to be added.

fromDataAnnotation bool

Indicates whether the configuration was specified using a data annotation.

Returns

IConventionEntityType

The new entity type.

Remarks

Shadow entities are not currently supported in a model that is used at runtime with a DbContext. Therefore, shadow state entity types will only exist in migration model snapshots, etc.

AddEntityType(string, string, IConventionEntityType, bool)

Adds an owned entity type with a defining navigation to the model.

IConventionEntityType? AddEntityType(string name, string definingNavigationName, IConventionEntityType definingEntityType, bool fromDataAnnotation = false)

Parameters

name string

The name of the entity type to be added.

definingNavigationName string

The defining navigation.

definingEntityType IConventionEntityType

The defining entity type.

fromDataAnnotation bool

Indicates whether the configuration was specified using a data annotation.

Returns

IConventionEntityType

The new entity type.

AddEntityType(string, Type, bool)

Adds a shared type entity type to the model.

IConventionEntityType? AddEntityType(string name, Type clrType, bool fromDataAnnotation = false)

Parameters

name string

The name of the entity to be added.

clrType Type

The CLR class that is used to represent instances of the entity type.

fromDataAnnotation bool

Indicates whether the configuration was specified using a data annotation.

Returns

IConventionEntityType

The new entity type.

Remarks

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.

AddEntityType(Type, bool)

Adds an entity type to the model.

IConventionEntityType? AddEntityType(Type type, bool fromDataAnnotation = false)

Parameters

type Type

The CLR class that is used to represent instances of the entity type.

fromDataAnnotation bool

Indicates whether the configuration was specified using a data annotation.

Returns

IConventionEntityType

The new entity type.

AddEntityType(Type, string, IConventionEntityType, bool)

Adds an owned entity type with a defining navigation to the model.

IConventionEntityType? AddEntityType(Type type, string definingNavigationName, IConventionEntityType definingEntityType, bool fromDataAnnotation = false)

Parameters

type Type

The CLR class that is used to represent instances of this entity type.

definingNavigationName string

The defining navigation.

definingEntityType IConventionEntityType

The defining entity type.

fromDataAnnotation bool

Indicates whether the configuration was specified using a data annotation.

Returns

IConventionEntityType

The new entity type.

AddIgnored(string, bool)

Marks the given entity type name as ignored.

string? AddIgnored(string typeName, bool fromDataAnnotation = false)

Parameters

typeName string

The name of the entity type to be ignored.

fromDataAnnotation bool

Indicates whether the configuration was specified using a data annotation.

Returns

string

The name of the ignored entity type.

AddIgnored(Type, bool)

Marks the given entity type as ignored.

string? AddIgnored(Type type, bool fromDataAnnotation = false)

Parameters

type Type

The entity type to be ignored.

fromDataAnnotation bool

Indicates whether the configuration was specified using a data annotation.

Returns

string

The name of the ignored entity type.

AddOwned(Type, bool)

Marks the given entity type as owned, indicating that when discovered entity types using the given type should be configured as owned.

void AddOwned(Type type, bool fromDataAnnotation = false)

Parameters

type Type

The type of the entity type that should be owned.

fromDataAnnotation bool

Indicates whether the configuration was specified using a data annotation.

AddOwnedEntityType(string, bool)

Adds an owned entity type of default type to the model.

IConventionEntityType? AddOwnedEntityType(string name, bool fromDataAnnotation = false)

Parameters

name string

The name of the entity to be added.

fromDataAnnotation bool

Indicates whether the configuration was specified using a data annotation.

Returns

IConventionEntityType

The new entity type.

Remarks

Shadow entities are not currently supported in a model that is used at runtime with a DbContext. Therefore, shadow state entity types will only exist in migration model snapshots, etc.

AddOwnedEntityType(string, Type, bool)

Adds an owned shared type entity type to the model.

IConventionEntityType? AddOwnedEntityType(string name, Type clrType, bool fromDataAnnotation = false)

Parameters

name string

The name of the entity to be added.

clrType Type

The CLR class that is used to represent instances of the entity type.

fromDataAnnotation bool

Indicates whether the configuration was specified using a data annotation.

Returns

IConventionEntityType

The new entity type.

Remarks

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.

AddOwnedEntityType(Type, bool)

Adds an owned entity type to the model.

IConventionEntityType? AddOwnedEntityType(Type type, bool fromDataAnnotation = false)

Parameters

type Type

The CLR class that is used to represent instances of the entity type.

fromDataAnnotation bool

Indicates whether the configuration was specified using a data annotation.

Returns

IConventionEntityType

The new entity type.

AddShared(Type, bool)

Marks the given entity type as shared, indicating that when discovered matching entity types should be configured as shared type entity type.

void AddShared(Type type, bool fromDataAnnotation = false)

Parameters

type Type

The type of the entity type that should be shared.

fromDataAnnotation bool

Indicates whether the configuration was specified using a data annotation.

DelayConventions()

Prevents conventions from being executed immediately when a metadata aspect is modified. All the delayed conventions will be executed after the returned object is disposed.

IConventionBatch DelayConventions()

Returns

IConventionBatch

An object that should be disposed to execute the delayed conventions.

Remarks

This is useful when performing multiple operations that depend on each other.

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.

IModel FinalizeModel()

Returns

IModel

The finalized model.

FindEntityType(string)

Gets the entity with the given name. Returns null if no entity type with the given name is found or the given CLR type is being used by shared type entity type or the entity type has a defining navigation.

IConventionEntityType? FindEntityType(string name)

Parameters

name string

The name of the entity type to find.

Returns

IConventionEntityType

The entity type, or null if none is found.

FindEntityType(string, string, IConventionEntityType)

Gets the entity type for the given name, defining navigation name and the defining entity type. Returns null if no matching entity type is found.

IConventionEntityType? FindEntityType(string name, string definingNavigationName, IConventionEntityType definingEntityType)

Parameters

name string

The name of the entity type to find.

definingNavigationName string

The defining navigation of the entity type to find.

definingEntityType IConventionEntityType

The defining entity type of the entity type to find.

Returns

IConventionEntityType

The entity type, or null if none is found.

FindEntityType(Type)

Gets the entity that maps the given entity class. Returns null if no entity type with the given name is found.

IConventionEntityType? FindEntityType(Type type)

Parameters

type Type

The type to find the corresponding entity type for.

Returns

IConventionEntityType

The entity type, or null if none is found.

FindEntityType(Type, string, IConventionEntityType)

Gets the entity type for the given name, defining navigation name and the defining entity type. Returns null if no matching entity type is found.

IConventionEntityType? FindEntityType(Type type, string definingNavigationName, IConventionEntityType definingEntityType)

Parameters

type Type

The type of the entity type to find.

definingNavigationName string

The defining navigation of the entity type to find.

definingEntityType IConventionEntityType

The defining entity type of the entity type to find.

Returns

IConventionEntityType

The entity type, or null if none is found.

FindEntityTypes(Type)

Gets the entity types matching the given type.

IEnumerable<IConventionEntityType> FindEntityTypes(Type type)

Parameters

type Type

The type of the entity type to find.

Returns

IEnumerable<IConventionEntityType>

The entity types found.

FindIgnoredConfigurationSource(string)

Indicates whether the given entity type name is ignored.

ConfigurationSource? FindIgnoredConfigurationSource(string typeName)

Parameters

typeName string

The name of the entity type that could be ignored.

Returns

ConfigurationSource?

The configuration source if the given entity type name is ignored, null otherwise.

FindIgnoredConfigurationSource(Type)

Indicates whether the given entity type is ignored.

ConfigurationSource? FindIgnoredConfigurationSource(Type type)

Parameters

type Type

The entity type that might be ignored.

Returns

ConfigurationSource?

The configuration source if the given entity type is ignored, null otherwise.

FindIsOwnedConfigurationSource(Type)

Returns the configuration source if the given type is marked as owned.

ConfigurationSource? FindIsOwnedConfigurationSource(Type type)

Parameters

type Type

The type of the entity type that could be owned.

Returns

ConfigurationSource?

The configuration source if the given type is marked as owned, null otherwise.

FindIsSharedConfigurationSource(Type)

Returns the configuration source if the given type is marked as shared.

ConfigurationSource? FindIsSharedConfigurationSource(Type type)

Parameters

type Type

The type that could be shared.

Returns

ConfigurationSource?

The configuration source if the given type is marked as shared, null otherwise.

FindLeastDerivedEntityTypes(Type, Func<IReadOnlyEntityType, bool>?)

Returns the entity types corresponding to the least derived types from the given one.

IEnumerable<IConventionEntityType> FindLeastDerivedEntityTypes(Type type, Func<IReadOnlyEntityType, bool>? condition = null)

Parameters

type Type

The base type.

condition Func<IReadOnlyEntityType, bool>

An optional condition for filtering entity types.

Returns

IEnumerable<IConventionEntityType>

List of entity types corresponding to the least derived types from the given one.

GetChangeTrackingStrategyConfigurationSource()

Returns the configuration source for GetChangeTrackingStrategy().

ConfigurationSource? GetChangeTrackingStrategyConfigurationSource()

Returns

ConfigurationSource?

The configuration source for GetChangeTrackingStrategy().

GetEntityTypes()

Gets all entity types defined in the model.

IEnumerable<IConventionEntityType> GetEntityTypes()

Returns

IEnumerable<IConventionEntityType>

All entity types defined in the model.

GetPropertyAccessModeConfigurationSource()

Returns the configuration source for GetPropertyAccessMode().

ConfigurationSource? GetPropertyAccessModeConfigurationSource()

Returns

ConfigurationSource?

The configuration source for GetPropertyAccessMode().

IsIgnored(string)

Indicates whether the given entity type name is ignored.

bool IsIgnored(string typeName)

Parameters

typeName string

The name of the entity type that could be ignored.

Returns

bool

true if the given entity type name is ignored.

IsIgnored(Type)

Indicates whether the given entity type is ignored.

bool IsIgnored(Type type)

Parameters

type Type

The entity type that might be ignored.

Returns

bool

true if the given entity type is ignored.

IsIgnoredType(Type)

Indicates whether entity types and properties with the given type should be ignored. This configuration is independent from IsIgnored(Type)

bool IsIgnoredType(Type type)

Parameters

type Type

The entity type that might be ignored.

Returns

bool

true if the given entity type is ignored.

IsOwned(Type)

Returns a value indicating whether the entity types using the given type should be configured as owned types when discovered.

bool IsOwned(Type type)

Parameters

type Type

The type of the entity type that could be owned.

Returns

bool

true if the given type is marked as owned, null otherwise.

RemoveEntityType(IConventionEntityType)

Removes an entity type from the model.

IConventionEntityType? RemoveEntityType(IConventionEntityType entityType)

Parameters

entityType IConventionEntityType

The entity type to be removed.

Returns

IConventionEntityType

The removed entity type, or null if the entity type was not found.

RemoveEntityType(string)

Removes an entity type without a defining navigation from the model.

IConventionEntityType? RemoveEntityType(string name)

Parameters

name string

The name of the entity type to be removed.

Returns

IConventionEntityType

The entity type that was removed.

RemoveEntityType(string, string, IConventionEntityType)

Removes an entity type with the given type, defining navigation name and the defining entity type.

IConventionEntityType? RemoveEntityType(string name, string definingNavigationName, IConventionEntityType definingEntityType)

Parameters

name string

The name of the entity type to be removed.

definingNavigationName string

The defining navigation.

definingEntityType IConventionEntityType

The defining entity type.

Returns

IConventionEntityType

The entity type that was removed.

RemoveEntityType(Type)

Removes an entity type from the model.

IConventionEntityType? RemoveEntityType(Type type)

Parameters

type Type

The entity type to be removed.

Returns

IConventionEntityType

The entity type that was removed.

RemoveEntityType(Type, string, IConventionEntityType)

Removes an entity type with the given type, defining navigation name and the defining entity type.

IConventionEntityType? RemoveEntityType(Type type, string definingNavigationName, IConventionEntityType definingEntityType)

Parameters

type Type

The CLR class that is used to represent instances of this entity type.

definingNavigationName string

The defining navigation.

definingEntityType IConventionEntityType

The defining entity type.

Returns

IConventionEntityType

The entity type that was removed.

RemoveIgnored(string)

Removes the ignored entity type name.

string? RemoveIgnored(string typeName)

Parameters

typeName string

The name of the ignored entity type to be removed.

Returns

string

The removed ignored type name.

RemoveOwned(Type)

Removes the given owned type, indicating that when discovered matching entity types should not be configured as owned.

string? RemoveOwned(Type type)

Parameters

type Type

The type of the entity type that should not be owned.

Returns

string

The name of the removed owned type.

RemoveShared(Type)

Marks the given type as not shared, indicating that when discovered matching entity types should not be configured as shared type entity types.

Type? RemoveShared(Type type)

Parameters

type Type

The type of the entity type that should be shared.

Returns

Type

The removed type.

SetChangeTrackingStrategy(ChangeTrackingStrategy?, bool)

Sets the default change tracking strategy to use for entities in the model. This strategy indicates how the context detects changes to properties for an instance of an entity type.

ChangeTrackingStrategy? SetChangeTrackingStrategy(ChangeTrackingStrategy? changeTrackingStrategy, bool fromDataAnnotation = false)

Parameters

changeTrackingStrategy ChangeTrackingStrategy?

The strategy to use.

fromDataAnnotation bool

Indicates whether the configuration was specified using a data annotation.

Returns

ChangeTrackingStrategy?

The configured value.

SetPropertyAccessMode(PropertyAccessMode?, bool)

Sets the PropertyAccessMode to use for properties of all entity types in this model.

PropertyAccessMode? SetPropertyAccessMode(PropertyAccessMode? propertyAccessMode, bool fromDataAnnotation = false)

Parameters

propertyAccessMode PropertyAccessMode?

The PropertyAccessMode, or null to clear the mode set.

fromDataAnnotation bool

Indicates whether the configuration was specified using a data annotation.

Returns

PropertyAccessMode?

The configured value.

Remarks

Note that individual entity types can override this access mode, and individual properties of entity types can override the access mode set on the entity type. The value set here will be used for any property for which no override has been specified.