Fluent API in Code-First:

We have seen different DataAnnotations attributes in the previous sections to override default Code-First Conventions. Here, we will learn about Fluent API.

Fluent API is another way to configure your domain classes. Fluent API provides more functionality for configuration than DataAnnotations. Fluent API supports the following types of mappings.

Mappings To Database
Model-wide Mapping
  • Set default Schema
  • Set Custom Convetions
Entity Mapping
  • To Single or Multiple Tables and Schema
  • To Complex type
  • Inheritance Hierarchies
Property Mapping
  • To Column, Column Name, Column Type, Nullable or Not Null Column, Column size, Columns Order
  • To Concurrency column
  • To Foreign key column
  • To configure relationships

Let's get started with Fluent API. First of all, let's create Student & Standard domain classes and context class as we have created in the Simple Code-First Example section. Now, override OnModelCreating method of DBContext in a context class, as shown below.

public class SchoolContext: DbContext
{
public SchoolDBContext(): base()
{
} public DbSet<Student> Students { get; set; }
public DbSet<Standard> Standards { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Configure domain classes using modelBuilder here base.OnModelCreating(modelBuilder);
}
}

Now, all your configuration code using Fluent API should be in OnModelCreating method. DbModelBuilder is a main class on which you can configure all your domain classes because at this point, all your domain classes would have initialized.

You can also use DataAnnotation and Fluent API at the same time. Code-First gives precedence to Fluent API > data annotations > default conventions.

DbModelBuilder class includes important properties and methods to configure. Visit MSDN for more information on DbModelBulder class.

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

Entity Framework Code-First(10):Fluent API的更多相关文章

  1. Entity Framework Tutorial Basics(10):Entity Lifecycle

    Entity Lifecycle: Before we work on CRUD operation (Create, Read, Update, Delete), it's important to ...

  2. Entity Framework Code first(转载)

    一.Entity Framework Code first(代码优先)使用过程 1.1Entity Framework 代码优先简介 不得不提Entity Framework Code First这个 ...

  3. Entity Framework Code First (三)Data Annotations

    Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表 ...

  4. Entity Framework Code First (二)Custom Conventions

    ---------------------------------------------------------------------------------------------------- ...

  5. Entity Framework Code First (一)Conventions

    Entity Framework 简言之就是一个ORM(Object-Relational Mapper)框架. Code First 使得你能够通过C#的类来描述一个模型,模型如何被发现/检测就是通 ...

  6. Entity Framework Tutorial Basics(11):Code First

    Code First development with Entity Framework: Entity Framework supports three different development ...

  7. Entity Framework Code First (七)空间数据类型 Spatial Data Types

    声明:本文针对 EF5+, Visual Studio 2012+ 空间数据类型(Spatial Data Types)是在 EF5 中引入的,空间数据类型表现有两种: Geography (地理学上 ...

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

    上篇博文说过当我们定义的类不能遵循约定(Conventions)的时候,Code First 提供了两种方式来配置你的类:DataAnnotations 和 Fluent API, 本文将关注 Flu ...

  9. Entity Framework Code First (八)迁移 Migrations

    创建初始模型和数据库 在开始使用迁移(Migrations)之前,我们需要一个 Project 和一个 Code First Model, 对于本文将使用典型的 Blog 和 Post 模型 创建一个 ...

随机推荐

  1. Python 案例一(计算人体体脂率)

    #计算人体体脂率 #输入部分 #身高 personHeight = input("请输入你的身高(m):") personHeight = float(personHeight) ...

  2. Java,Hibernate,标准sql数据类型之间的对应表

    (2010-07-14 19:53:50) 转载▼ 标签: 杂谈 分类: JAVA Hibernate API简介 其接口分为以下几类: l         提供访问数据库的操作的接口: l      ...

  3. 2014年的暑假ACM之旅!

    致未来的我: 回到学校了,又开始了繁忙的生活! 虽然每天都不太轻松,但还是蛮有乐趣的,一起讨论某道题或者某种算法时挺开心的.@我.@姜维波.@曹彦宝.@李岩.@张永宏 继续这样下去,直到这个暑假的结束 ...

  4. Kafka- Spark消费Kafka

    在高版本的API中 val brokers = properties.getProperty("kafka.host.list") val topics = Set(propert ...

  5. jQuery cookie 实现记住用户名和密码功能

    jQuery cookie 实现记住用户名和密码功能 HTML代码 <div class="wrap"> <div class="line-top&qu ...

  6. JS获取内联样式

    JS获取内联样式 //获取内联样式 function getCss(obj,attr){//obj:对象,name:style属性 if(obj.currentStyle) { return obj. ...

  7. CSS控制表格嵌套

    网页设计应用中,当我们不能完全放弃表格的使用时,为了达到预期的效果,不免要用到表格嵌套(特别是多层嵌套)方式来进行布局.可能很多同仁都遇到过这样的问题,为了达到显示效果要为每一个(每一层)的表格写不同 ...

  8. 英语发音规则---Y字母

    英语发音规则---Y字母 一.总结 一句话总结: 1.Y字母在单词最前面读发[j]? yes /jes/ [jɛs] n. 是 yard /jɑːd/ [jɑd] n. 院子 yellow /'jel ...

  9. QuickReport FastReport

    一.QuickReport1.安装Component->Install packages->X:/Program Files/Borland/Delphi7/Bin/dclqrt70.bp ...

  10. 单机版 RedisUtils({基本操作封装工具类})【三】

    <!--集成的RedisJAR--> <!--引入jedis需的jar包--> <dependency> <groupId>redis.clients& ...