Code First:Fluent API
DbContext类有一个OnModelCreating方法,可以在这里配置模型,该方法接收一个类型为DbModelBuilder的建造者,本文介绍的为Data Anotation的等价方法,这些代码是自解释的。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Configure Code First to ignore PluralizingTableName convention
// If you keep this convention then the generated tables will have pluralized names.
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); // Default Schema (EF6 onwards)
modelBuilder.HasDefaultSchema("sales"); // Configuring a Primary Key [Key]
modelBuilder.Entity<OfficeAssignment>().HasKey(t => t.InstructorID); // Configuring a Composite Primary Key
modelBuilder.Entity<Passport>().HasKey(t => new { t.PassportNumber, t.IssuingCountry }); // Switching off Identity for Numeric Primary Keys
modelBuilder.Entity<Passport>().Property(t => t.PassportNumber).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); // Specifying the Maximum Length on a Property
modelBuilder.Entity<Passport>().Property(t => t.IssuingCountry).HasMaxLength(); // Configuring the Property to be Required
// (NOT NULL) [Required]
modelBuilder.Entity<Passport>().Property(t => t.Issued).IsRequired(); // Specifying Not to Map a CLR Property to a Column in the Database
// [NotMapped]
modelBuilder.Entity<Blog>().Ignore(t => t.BlogCode); // Mapping a CLR Property to a Specific Column in the Database
// [Column("BlogTitle")]
modelBuilder.Entity<Blog>().Property(t => t.Title).HasColumnName("BlogTitle"); // Configuring whether a String Property Supports Unicode Content
// (varchar not nvarchar)
modelBuilder.Entity<Blog>().Property(t => t.Title).IsUnicode(false); // Configuring the Data Type of a Database Column [Column(TypeName = "varchar")]
modelBuilder.Entity<Department>().Property(p => p.Name).HasColumnType("varchar"); // Mapping an Entity Type to a Specific Table in the Database
modelBuilder.Entity<Department>().ToTable("t_Department"); // 第二个参数是SCHEMA
modelBuilder.Entity<Department>().ToTable("t_Department", "school"); // Specifying That a Class Is a Complex Type
modelBuilder.ComplexType<Details>(); // Specifying Not to Map a CLR Entity Type to a Table in the Database
// [NotMapped]
modelBuilder.Ignore<OnlineCourse>(); // Configuring Properties on a Complex Type
// Method 1
modelBuilder.ComplexType<Details>().Property(t => t.Location).HasMaxLength();
// Method 2
modelBuilder.Entity<OnsiteCourse>().Property(t => t.Details.Location).HasMaxLength(); // Configuring a Property to Be Used as an Optimistic Concurrency Token
modelBuilder.Entity<OfficeAssignment>().Property(t => t.Timestamp).IsConcurrencyToken(); // Configuring the property to be a row version in the database
modelBuilder.Entity<OfficeAssignment>().Property(t => t.Timestamp).IsRowVersion();
}
Code First:Fluent API的更多相关文章
- Code First 关系 Fluent API
通过实体框架 Code First,可以使用您自己的域类表示 EF 执行查询.更改跟踪和更新函数所依赖的模型.Code First 利用称为“约定先于配置”的编程模式.这意味着 Code First ...
- Code First约定-Fluent API配置
转自:http://blog.163.com/m13864039250_1/blog/static/2138652482015283397609/ 用Fluent API 配置/映射属性和类型 简介 ...
- EF:Fluent API 把一对多映射为一对一
假设有两张表:A表和B表.A表与B表在数据库中的关系是一对多,但我们需要在EF中映射为一对一. 首先在A实体类和B实体类中互相为对方增加一个实体类的属性: public A { public B B ...
- code First 三 Fluent API
Entity Framework Fluent API用于配置域类以覆盖约定. 在实体框架6中,DbModelBuilder类充当Fluent API,我们可以使用它来配置许多不同的东西.它提供了比数 ...
- Entity Framework Code-First(10):Fluent API
Fluent API in Code-First: We have seen different DataAnnotations attributes in the previous sections ...
- Entity Framework(七):Fluent API配置案例
一.配置主键 要显式将某个属性设置为主键,可使用 HasKey 方法.在以下示例中,使用了 HasKey 方法对 Product 类型配置 ProductId 主键. 1.新加Product类 usi ...
- 1.【使用EF Code-First方式和Fluent API来探讨EF中的关系】
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/relationship-in-entity-framework-using-code-firs ...
- 8.Fluent API in Code-First【Code-First系列】
在前面的章节中,我们已经看到了各种不同的数据注解特性.现在我们来学习一下Fluent API. Fluent API是另外一种配置领域类的方式,它提供了更多的配置相比数据注解特性. Mappings[ ...
- Code First :使用Entity. Framework编程(2) ----转发 收藏
第二章:Code First概览 如果你使用第一.二版的EF框架工作过,你会回想起书中的业务案例:Break Away Geek Adventures, 简称BAGA.BAGA共享了很多像我们这样的奇 ...
随机推荐
- LeetCode(62)Unique Paths
题目 A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- 关于Filter中ServletRequest和ServletResponse强转HttpServletRequest和HttpServletResponse安全问题(向下转型一定不安全吗?)
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOE ...
- Leetcode 174.地下城游戏
地下城游戏 一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格.我们英勇的骑士(K)最初被安置在左上角的房间里,他必须穿过地下城并通过对抗恶魔来拯救公主. ...
- HDU1811 并查集+拓扑排序
题目大意: 判断是否能根据给定的规则将这一串数字准确排序出来 我们用小的数指向大的数 对于相等的情况下,将二者合并到同一个并查集中,最后抽象出来的图上面的每一个点都应该代表并查集的标号 #includ ...
- mysql启服务的时候报1067错误解决办法
网上百度了半天没有有效的办法,推荐一个万能的办法: 1.看日志: mysql安装目录下 data文件 -> 后缀为.err 的文件就是日志文件 打开它 : 可以看到错误信息 2.看错 ...
- World is Exploding 树状数组+离散化
Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies: a≠b≠c≠d,1≤a<b≤n,1≤ ...
- 洛谷——P1062 数列
洛谷——P1062 数列 题目描述 给定一个正整数k(3≤k≤15),把所有k的方幂及所有有限个互不相等的k的方幂之和构成一个递增的序列,例如,当k=3时,这个序列是: 1,3,4,9,10,12,1 ...
- 浅识MySQL
MySQL常用语句 #操作数据库 ##创建数据库 CREATE DATABASE `dbName`; ##切换数据库 USE `dbName`; ##查看所有数据库 SHOW DATABASES; # ...
- 英特尔固态盘 说明书PDF
http://www.intel.cn/content/www/cn/zh/solid-state-drives/solid-state-drives-ssd.html
- 【CV论文阅读】Detecting events and key actors in multi-person videos
论文主要介绍一种多人协作的视频事件识别的方法,使用attention模型+RNN网络,最近粗浅地学习了RNN网络,它比较适合用于处理序列的存在上下文作用的数据. NCAA Basketball数据集 ...