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. MySQL主从配置详解

    一.mysql主从原理 1. 基本介绍 MySQL 内建的复制功能是构建大型,高性能应用程序的基础.将 MySQL 的 数亿分布到到多个系统上去,这种分步的机制,是通过将 MySQL 的某一台主机的数 ...

  2. MySQL-----增

    增 **创建用户** create user 'alex'@'192.168.1.1' identified by '123123'; create user 'alex'@'192.168.1.%' ...

  3. java 编码乱码问题

    Tomcat的server.xml 文件Connector标签加上URIEncoding="utf-8": <Connector port=" protocol=& ...

  4. Unity Water Shader

    上图是一个物体浸入水中的效果 原理 我们使用相机渲染的整个场景的深度图减去需要忽略的模型的深度,这里忽略的是图中蓝色部分,就保留了其他的深度值. 用到Main Camera渲染的深度贴图: sampl ...

  5. NYOJ-851寻找最大数(二),栈贪心!

    寻找最大数(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 给你一个数字n(可能有前缀0). 要求从高位到低位,进行 进栈出栈 操作,是最后输出的结果最大. 输入 ...

  6. CodeForces - 425E Sereja and Sets 题解

    题目大意: 我们有一个集合 S,其中包含了 m 个不完全相同的区间[l1,r1],[l2,r2]…[lm,rm] (1≤li≤ri≤n,li,ri 都为整数). 定义 f(S)=k,表示集合 S 中能 ...

  7. Linux下汇编语言学习笔记10 ---

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

  8. poj——2367  Genealogical tree

    Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6025   Accepted: 3969 ...

  9. ppc_85xx-gcc -shared -fPIC liberr.c -o liberr.so

    fPIC作用于编译阶段,告诉编译器产生与位置无关代码(Position-Independent Code),   则产生的代码中,没有绝对地址,所有使用相对地址.故而代码能够被载入器载入到内存的随意 ...

  10. 将github上的项目源码导入eclipse详细教程

    将github上的项目源码导入eclipse详细教程 学习了: http://blog.csdn.net/itbiggod/article/details/78462720