Entity Framework Code-First(10):Fluent API
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 |
|
Entity Mapping |
|
Property Mapping |
|
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的更多相关文章
- Entity Framework Tutorial Basics(10):Entity Lifecycle
Entity Lifecycle: Before we work on CRUD operation (Create, Read, Update, Delete), it's important to ...
- Entity Framework Code first(转载)
一.Entity Framework Code first(代码优先)使用过程 1.1Entity Framework 代码优先简介 不得不提Entity Framework Code First这个 ...
- Entity Framework Code First (三)Data Annotations
Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表 ...
- Entity Framework Code First (二)Custom Conventions
---------------------------------------------------------------------------------------------------- ...
- Entity Framework Code First (一)Conventions
Entity Framework 简言之就是一个ORM(Object-Relational Mapper)框架. Code First 使得你能够通过C#的类来描述一个模型,模型如何被发现/检测就是通 ...
- Entity Framework Tutorial Basics(11):Code First
Code First development with Entity Framework: Entity Framework supports three different development ...
- Entity Framework Code First (七)空间数据类型 Spatial Data Types
声明:本文针对 EF5+, Visual Studio 2012+ 空间数据类型(Spatial Data Types)是在 EF5 中引入的,空间数据类型表现有两种: Geography (地理学上 ...
- Entity Framework Code First (四)Fluent API - 配置属性/类型
上篇博文说过当我们定义的类不能遵循约定(Conventions)的时候,Code First 提供了两种方式来配置你的类:DataAnnotations 和 Fluent API, 本文将关注 Flu ...
- Entity Framework Code First (八)迁移 Migrations
创建初始模型和数据库 在开始使用迁移(Migrations)之前,我们需要一个 Project 和一个 Code First Model, 对于本文将使用典型的 Blog 和 Post 模型 创建一个 ...
随机推荐
- 【leetcode刷题笔记】Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- Docker 架构篇
Docker 的核心组件包括: Docker 客户端 - Client Docker 服务器 - Docker daemon Docker 镜像 - Image Registry Docker 容器 ...
- Mysql视图使用总结
视图View使用总结: 视图可以看作为“虚拟表”,因为它返回的结果集格式与实体数据表返回的数据集格式类似,并且引用视图的方式与引用数据表的方式相同.每次查询使用视图时,DBMS会动态生成视图结果集所需 ...
- LINQ 学习路程 -- 查询操作 Deferred Execution of LINQ Query 延迟执行
延迟执行是指一个表达式的值延迟获取,知道它的值真正用到. 当你用foreach循环时,表达式才真正的执行. 延迟执行有个最重要的好处:它总是给你最新的数据 实现延迟运行 你可以使用yield关键字实现 ...
- git use
git init git init --bare ''会将文档直接加上.git后缀 具体内容请渡 git add --all git commit -m 'label' git push --all ...
- 分享知识-快乐自己:IO流基本操作
点我参考常用API: IO图解: 相关理论: 流的概念和作用: 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将 ...
- 分享知识-快乐自己:SpringBoot集成热部署配置(一)
摘要: 热部署与热加载: ava热部署与Java热加载的联系和区别: 1):Java热部署与热加载的联系: 1.不重启服务器编译/部署项目 2.基于Java的类加载器实现 2):Java热部署与热加载 ...
- C++(二)— STL容器的基本用法
1.vector基本操作 关于vector简单的讲就是一个动态增长的数组,里面有一个指针指向一片连续的内存空间,当空间装不下的时候会自动申请一片更大的空间(空间配置器)将原来的数据拷贝到新的空间,然后 ...
- windows 批处理ping ip
//pingSingleIp ;;@Echo off @for /f "tokens=1-4 delims=." %%i in (ip.txt) do (@ping -w 600 ...
- SpringBoot_异常_01_Caused by: java.lang.BootstrapMethodError: java.lang.NoSuchMethodError
一.异常信息 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gsonB ...