Tuesday, December 10, 2013

EntityTypeConfiguration Class in Code-First

It is important to understand EntityTypeConfiguration class because it is an important class that allows configuration to be performed for an entity type in a model. It can be obtained by calling Entity method of DbModelBuilder class:

EntityTypeConfiguration

EntityTypeConfiguration has following important methods:

Method NameReturn TypeDescription
HasKey<TKey>EntityTypeConfigurationConfigures the primary key property(s) for this entity type.
HasMany<TTargetEntity>ManyNavigationPropertyConfigurationConfigures a many relationship from this entity type.
HasOptional<TTargetEntity>OptionalNavigationPropertyConfigurationConfigures an optional relationship from this entity type. Instances of the entity type will be able to be saved to the database without this relationship being specified. The foreign key in the database will be nullable.
HasRequired<TTargetEntity>RequiredNavigationPropertyConfigurationConfigures a required relationship from this entity type. Instances of the entity type will not be able to be saved to the database unless this relationship is specified. The foreign key in the database will be non-nullable.
Ignore<TProperty>VoidExcludes a property from the model so that it will not be mapped to the database.
MapEntityTypeConfigurationAllows advanced configuration related to how this entity type is mapped to the database schema.
Property<T>StructuralTypeConfigurationConfigures a struct property that is defined on this type.
ToTableVoidConfigures the table name that this entity type is mapped to.

Visit MSDN for more information on EntityTypeConfiguration class.

No comments:

Post a Comment