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. linux上uwsgi+nginx+django发布项目

    在发布项目前首先将部署环境进行搭建,尤其是依赖包一定需要提前安装. 一.虚拟环境的搭建 1.建议在linux下新建一个虚拟环境,这样有独立干净的环境. mkvirtualenv -p python3 ...

  2. 集训第四周(高效算法设计)F题 (二分+贪心)

    Description   A set of n<tex2html_verbatim_mark> 1-dimensional items have to be packed in iden ...

  3. mac 文本处理命令分享

    mac 文本处理命令分享 */--> pre.src {background-color: #292b2e; color: #b2b2b2;} pre.src {background-color ...

  4. BNUOJ 7178 病毒侵袭持续中

    病毒侵袭持续中 Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 30 ...

  5. python 几种点积运算方式效率分析

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51793984 本文列举出几种pytho ...

  6. 【最大流】Escape

    https://www.bnuoj.com/v3/contest_show.php?cid=9149#problem/F [题意] 给定n个人和m个星球,每个人可以匹配某些星球,每个星球有一定的容量限 ...

  7. 前端开发:JavaScript---DOM & BOM

    DOM:Document Object Model  文档对象类型 模态框案例 <!DOCTYPE html> <html lang="en"> <h ...

  8. linux程序分析工具

    ldd和nm是Linux下两个非常实用的程序分析工具.ldd是用来分析程序运行时需要依赖的动态链接库的工具,nm是用来查看指定程序中的符号表信息的工具,objdump用来查看源代码与汇编代码,-d只查 ...

  9. Linux下汇编语言学习笔记44 ---

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

  10. baidu 和 es 使用

    http://www.cnblogs.com/kangoroo/p/8047586.html