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的更多相关文章

  1. Code First 关系 Fluent API

    通过实体框架 Code First,可以使用您自己的域类表示 EF 执行查询.更改跟踪和更新函数所依赖的模型.Code First 利用称为“约定先于配置”的编程模式.这意味着 Code First ...

  2. Code First约定-Fluent API配置

    转自:http://blog.163.com/m13864039250_1/blog/static/2138652482015283397609/ 用Fluent API 配置/映射属性和类型 简介 ...

  3. EF:Fluent API 把一对多映射为一对一

    假设有两张表:A表和B表.A表与B表在数据库中的关系是一对多,但我们需要在EF中映射为一对一. 首先在A实体类和B实体类中互相为对方增加一个实体类的属性: public A { public B B ...

  4. code First 三 Fluent API

    Entity Framework Fluent API用于配置域类以覆盖约定. 在实体框架6中,DbModelBuilder类充当Fluent API,我们可以使用它来配置许多不同的东西.它提供了比数 ...

  5. Entity Framework Code-First(10):Fluent API

    Fluent API in Code-First: We have seen different DataAnnotations attributes in the previous sections ...

  6. Entity Framework(七):Fluent API配置案例

    一.配置主键 要显式将某个属性设置为主键,可使用 HasKey 方法.在以下示例中,使用了 HasKey 方法对 Product 类型配置 ProductId 主键. 1.新加Product类 usi ...

  7. 1.【使用EF Code-First方式和Fluent API来探讨EF中的关系】

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/relationship-in-entity-framework-using-code-firs ...

  8. 8.Fluent API in Code-First【Code-First系列】

    在前面的章节中,我们已经看到了各种不同的数据注解特性.现在我们来学习一下Fluent API. Fluent API是另外一种配置领域类的方式,它提供了更多的配置相比数据注解特性. Mappings[ ...

  9. Code First :使用Entity. Framework编程(2) ----转发 收藏

    第二章:Code First概览 如果你使用第一.二版的EF框架工作过,你会回想起书中的业务案例:Break Away Geek Adventures, 简称BAGA.BAGA共享了很多像我们这样的奇 ...

随机推荐

  1. JavaScript中的特殊数据类型

    JavaScript中的特殊数据类型 制作人:全心全意 转义字符 以反斜杠开头的不可显示的特殊字符通常为控制字符,也被称为转义字符.通常转义字符可以在字符串中添加不可显示的特殊字符,或者防止引号匹配混 ...

  2. 树莓派 -- bcm2835 library (1)

    bcm2835 library提供了user space 操作IO的代码. 本文不涉及代码分析,先直观的按照user guide完成操作. 1. 在Raspberry中安装bcm2835 librar ...

  3. Python 3安装体验篇(win10)

    一.下载 1.打开官网https://www.python.org/downloads/windows/,点击Python 3版本链接 2.点击win10 64位安装链接,即可下载Python安装 二 ...

  4. api安全认证

    三.auth自定义授权 客户端代码: import requests import hashlib import time current_time = time.time() #自意义的字符串app ...

  5. Leetcode 137.只出现一次的数字II

    只出现一次的数字II 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现了三次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? ...

  6. Linux下汇编语言学习笔记0 --- 前期准备工作

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  7. POJ 3281_Dining

    题意: FJ准备了F种食物和D种饮料,每头牛都有喜欢的食物和饮料,并且每头牛都只能分配一种食物和饮料.问如何分配使得同时得到喜欢的食物和饮料的牛数量最多. 分析: 首先想到将牛与其对应的食物和饮料匹配 ...

  8. [bzoj3436]小K的农场_差分约束

    小K的农场 bzoj-3436 题目大意:给定n个点,每个节点有一个未知权值.现在有m个限制条件,形如:点i比点j至少大c,点i比点j至多大c或点i和点j相等.问是否可以通过给所有点赋值满足所有限制条 ...

  9. FJNUOJ1158(莫比乌斯反演)

    题目:给定n个数字a1...an.有m个询问,格式为L R X Y,意为求aL到aR之间与x的最大公因数为y的个数. 数据组数T<=20 1<=n,m<=1e5 1<=ai&l ...

  10. 图片在 canvas 中的 选中/平移/缩放/旋转,包含了所有canvas的2D变化,让你认识到数学的重要性

    1.介绍 canvas 已经出来好久了,相信大家多少都有接触. 如果你是前端页面开发/移动开发,那么你肯定会有做过图片上传处理,图片优化,以及图片合成,这些都是可以用 canvas 实现的. 如果你是 ...