Entity Framework Code-First(10.1):EntityTypeConfiguration

EntityTypeConfiguration Class in Code-First:

Before we start to configure using Fluent API, let‘s see an important class of Fluent API.

EntityTypeConfiguration is an important class in Fluent API. EntityTypeConfiguration provides you important methods to configure entities and its properties to override various Code-First conventions. It can be obtained by calling the Entity<TEntity>() method of DbModelBuilder class as shown below.

EntityTypeConfiguration has the following important methods:

Method Name Return Type Description
HasKey<TKey> EntityTypeConfiguration Configures the primary key property(s) for this entity type.
HasMany<TTargetEntity> ManyNavigationPropertyConfiguration Configures 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.
Map EntityTypeConfiguration 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.
ToTable Void Configures the table name that this entity type is mapped to.

Visit MSDN for more information on EntityTypeConfiguration class.

Let‘s start to configure entities using Fluent API in the next section.

时间: 2024-10-10 11:11:21

Entity Framework Code-First(10.1):EntityTypeConfiguration的相关文章

Entity Framework Code First (三)Data Annotations

Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表示 EF 所依赖的模型去执行查询.更改追踪.以及更新功能,这意味着你的 domain classes 必须遵循 EF 所使用的约定.然而,如果你的 domain classes 不能遵循 EF 所使用的约定,此时你就需要有能力去增加一些配置使得你的 classes 能够满足 EF 所需要的信息. C

Entity Framework Code First (二)Custom Conventions

------------------------------------------------------------------------------------------------------------ 注意:以下所讨论的功能或 API 等只针对 Entity Framework 6 ,如果你使用早期版本,可能部分或全部功能不起作用! --------------------------------------------------------------------------

Entity Framework Code First (一)Conventions

Entity Framework 简言之就是一个ORM(Object-Relational Mapper)框架. Code First 使得你能够通过C#的类来描述一个模型,模型如何被发现/检测就是通过一些约定(Conventions).Conventions 就是一系列规则的集合,被用于对基于类别定义的概念模型的自动装配. 这些约定都被定义于 System.Data.Entity.ModelConfiguration.Conventions 命名空间下. 当然你可以进一步地对你的模型作出配置,

Entity Framework Code First (四)Fluent API - 配置属性/类型

小分享:我有几张阿里云优惠券,用券购买或者升级阿里云相应产品最多可以优惠五折!领券地址:https://promotion.aliyun.com/ntms/act/ambassador/sharetouser.html?userCode=ohmepe03 上篇博文说过当我们定义的类不能遵循约定(Conventions)的时候,Code First 提供了两种方式来配置你的类:DataAnnotations 和 Fluent API, 本文将关注 Fluent API.  一般来说我们访问 Flu

Entity Framework Code First (八)迁移 Migrations

参考页面: http://www.yuanjiaocheng.net/entity/Persistence-in-EF.html http://www.yuanjiaocheng.net/entity/crud-in-connected.html http://www.yuanjiaocheng.net/entity/crud-in-Disconnected.html http://www.yuanjiaocheng.net/entity/add-entity-in-disconnected.h

Entity Framework Code First (五)Fluent API - 配置关系

上一篇文章我们讲解了如何用 Fluent API 来配置/映射属性和类型,本文将把重点放在其是如何配置关系的. 文中所使用代码如下 public class Student { public int ID { get; set; } public string Name { get; set; } public DateTime EnrollmentDate { get; set; } // Navigation properties public virtual Address Address

SQL Server安全(10/11):行级别安全(Row-Level Security)

在保密你的服务器和数据,防备当前复杂的攻击,SQL Server有你需要的一切.但在你能有效使用这些安全功能前,你需要理解你面对的威胁和一些基本的安全概念.这篇文章提供了基础,因此你可以对SQL Server里的安全功能充分利用,不用在面对特定威胁,不能保护你数据的功能上浪费时间. 不像其它一些工业强度的数据库服务器.SQL Server对于单个数据记录,缺少内建机制,称作行级别安全(Row-Level Security).这篇文章会探寻为什么你可能想使用这样的行级别颗粒的数据访问安全和你如何能

Entity Framework Code-First(10.2):Entity Mappings

Entity Mappings using Fluent API: Here, we will learn how to configure an entity using Fluent API. We will use the following Student and Standard domain classes of the school application. public class Student { public Student() { } public int Student

ADO.NET Entity Framework -Code Fisrt 开篇(一)

ADO.NET Entity Framework -Code Fisrt 开篇(一) 2012-12-25 15:13 by 易code, 911 阅读, 0 评论, 收藏, 编辑 ADO.NET Entity Framework 是微软的一套实体映射框架.发布EF4.1(Entity Framework )时,又提出了代码先行的设计理念(the code comes first, the rest follows).具体好处哪是多多,查资料吧. 参考资料:Programming Entity