8.1 EntityTypeConfiguration Class in Code-First【Code First系列】

本文介绍了FluentAPI中的重要类EntityTypeConfiguration,详细解释了如何通过该类配置领域类的属性和关系,包括配置主键、多对多关系、可选及必须关系等。

在我们学习Fluent API之前,先来看看Fluent API中重要的类--EntityTypeConfiguration吧。

EntityTypeConfiguration类是Fluent API 中重要的类,这个类提供了重要的属性和方法来配置领域类,让我们可以不用按照约定的Code-First那样的配置来,配置我们的领域类。

EntityTypeConfiguration类型可以通过调用DbModelBuilder类中的Entity<TEntity>()泛型方法得到。

EntityTypeConfiguration类,有下面比较重要的方法。

Method NameReturn TypeDescription
HasKey<TKey>EntityTypeConfigurationConfigures the primary key property(s) for this entity type.【配置主键】
HasMany<TTargetEntity>ManyNavigationPropertyConfigurationConfigures a many-to-many relationship from this entity type.【配置多对多的关系】
HasOptional<TTargetEntity>OptionalNavigationPropertyConfiguration

Configures an optional relationship from this entity type. Instances of the entity type can be saved to the database without this relationship being specified. The foreign key in the database will be nullable.

【配置可选的关系,实体的类型可以被保存到数据库,不用特别指定关系,数据库中的外键列将会是可空的。】

HasRequired<TTargetEntity>RequiredNavigationPropertyConfiguration

Configures 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>Void

Excludes a property from the model so that it will not be mapped to the database.

【标注了这个Ignore特性的属性,不会映射到数据库中。】

MapEntityTypeConfiguration

Allows advanced configuration related to how this entity type is mapped to the database schema.

【允许高级的配置将这个实体类型映射成数据表】

Property<T>StructuralTypeConfiguration

Configures a struct property that is defined on this type.

【配置属性】

ToTableVoid

Configures the table name that this entity type is mapped to.

【配置实体,映射成的表名】

要了解更多关于这个EntityTypeConfiguration类的信息,请看MSDN, EntityTypeConfiguration

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值