一丶首先新建两个实体类

public class Student
{
public int StudentKey { get; set; }
public string StudentName { get; set; }
public DateTime DateOfBirth { get; set; }
public byte[] Photo { get; set; }
public decimal Height { get; set; }
public float Weight { get; set; } public Standard Standard { get; set; }
} public class Standard
{
public int StandardKey { get; set; }
public string StandardName { get; set; } public ICollection<Student> Students { get; set; }
}

二丶设置主键和复合主键(实体配置均在重写虚方法【OnModelCreating】里面)

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 primary key[配置主键]
modelBuilder.Entity<Student>().HasKey<int>(s => s.StudentKey);
modelBuilder.Entity<Standard>().HasKey<int>(s => s.StandardKey); //Configure composite primary key【配置复合主键】
modelBuilder.Entity<Student>().HasKey<int>(s => new { s.StudentKey, s.StudentName });
}
}

三丶配置列名称、类型和顺序

 modelBuilder.Entity<Student>()
.Property(p => p.DateOfBirth)
.HasColumnName("DoB")
.HasColumnOrder()
.HasColumnType("datetime2");

四丶配置不为空和为空的情况

            //Configure Null Column
modelBuilder.Entity<Student>()
.Property(p => p.Heigth)
.IsOptional(); //Configure NotNull Column
modelBuilder.Entity<Student>()
.Property(p => p.Weight)
.IsRequired();

五丶配置列的大小

     //Set StudentName column size to 50
modelBuilder.Entity<Student>()
.Property(p => p.StudentName)
.HasMaxLength(); //Set StudentName column size to 50 and change datatype to nchar
//IsFixedLength() change datatype from nvarchar to nchar
modelBuilder.Entity<Student>()
.Property(p => p.StudentName)
.HasMaxLength().IsFixedLength(); //Set size decimal(2,2)
modelBuilder.Entity<Student>()
.Property(p => p.Height)
.HasPrecision(, );

六丶配置并发列(乐观锁)

        //Set StudentName as concurrency column
modelBuilder.Entity<Student>()
.Property(p => p.StudentName)
.IsConcurrencyToken();

正如您在上面的示例中看到的,我们将StudentName列设置为并发列,以便将其包括在update和delete命令中的where子句中。

还可以使用IsRowVersion()方法来将字节[]类型属性作为并发列。

this.Property(p => p.TimeStramp).IsRowVersion();

七丶将学生实体映射到数据库中的多个表。

            modelBuilder.Entity<Student>().Map(m =>
{
m.Properties(p => new { p.StudentId, p.StudentName});
m.ToTable("StudentInfo");
}).Map(m => {
m.Properties(p => new { p.StudentId, p.Height, p.Weight, p.Photo, p.DateOfBirth});
m.ToTable("StudentInfoDetail");
}); modelBuilder.Entity<Standard>().ToTable("StandardInfo");

八丶设置自增列

 modelBuilder.Entity<Student>.Property(s => s.id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);//设置自增列

 modelBuilder.Entity<Student>.Property(s => s.id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);//设置非自增列

参考

CodeFrist基础_Fluent Api的更多相关文章

  1. BotVS开发基础—Python API

    代码 import json def main(): # python API列表 https://www.botvs.com/bbs-topic/443 #状态信息 LogStatus(" ...

  2. NodeJS:(二)基础常用API

    node.js中文网:http://nodejs.cn/api/ (path.Buffer.events.fs) ①path路径-----const {resolve} = require('path ...

  3. Java 基础 常用API (System类,Math类,Arrays, BigInteger,)

    基本类型包装类 基本类型包装类概述 在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的.而程序开发中,我们需要把字符串数据,根据需求转换成指定的基本数据类型,如年龄需要转换成int类 ...

  4. Java 基础 常用API ( 正则表达式,Date类,DateFormat类,Calendar类 )

    正则表达式 正则表达式的概念 正则表达式(英语:Regular Expression,在代码中常简写为regex). 正则表达式是一个字符串,使用单个字符串来描述.用来定义匹配规则,匹配一系列符合某个 ...

  5. Java 基础 常用API (Object类,String类,StringBuffer类)

    Java API Java 的API(API: Application(应用) Programming(程序) Interface(接口)) Java API就是JDK中提供给我们使用的类,这些类将底 ...

  6. JAVA基础--JAVA API常见对象(其他API)13

    一.其他API 1.System类 system类中的方法和成员变量都是静态的, 不需要创建System对象就可以直接使用. /* * 演示System的使用 */ public class Syst ...

  7. 软件定义网络基础---REST API概述

    一:什么是REST API REST API是北向接口的主流设计方式 API是应用程序编程接口,是预先定义好的函数,可以供应用程序或开发人员访问调用 年 Roy Thomas Fielding 的博士 ...

  8. 软件定义网络基础---REST API的设计规范

    一:REST API的设计 REST API是基于HTTP协议进行设计的,由HTTP动词+URI组成 (一)HTTP动词 (二)资源的原型 文档(Document): 文档是资源的单一表现形式: 集合 ...

  9. elasticsearch 基础 —— Update API

    Update API 更新API允许基于提供的脚本更新文档.该操作从索引获取文档(与分片并置),运行脚本(使用可选的脚本语言和参数),并对结果进行索引(也允许删除或忽略操作).它使用版本控制来确保在& ...

随机推荐

  1. CGlib小记

    CGlib是一个强大的代码生成包.常被用于各种AOP框架,提供"拦截"功能. JDK本身就为控制要訪问的对象提供了一 种途径,动态代理Proxy. 可是被代理的累必须实现一个或多个 ...

  2. Restful WebService简介

    RESTful Web Services已经渐渐開始流行, 主要是用于解决异构系统之间的通信问题.非常多站点和应用提供的API,都是基于RESTful风格的Web Services,比較就有Googl ...

  3. LeetCode145 Binary Tree Postorder Traversal Java题解(递归 迭代)

    题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...

  4. 呐喊-Skrik

    尼斯,1892年1月22日,我和两个朋友还在散步,太阳已快下山了,天空突然间变得血一样红,我似乎感受到了一种悲伤忧郁的气息,我止住了脚步,轻轻地倚在篱笆边,极度的疲倦已使我快要窒息了.火焰般的云彩像血 ...

  5. USACO Section 1.2PROB Transformations

    挺有趣的一道题,呵呵,不算难 /* ID: jusonal1 PROG: transform LANG: C++ */ #include <iostream> #include <f ...

  6. Spring 框架学习 —— 容器

    容器是 Spring 框架的核心.Spring 容器使用 DI(依赖注入)机制管理构成应用的组件(类),所谓 DI,也即是其能够创建相互协作的组件(类)之间的关联(依赖). 1. 应用上下文(Appl ...

  7. 如何精通javascript

    http://stackoverflow.com/questions/2628672/what-should-every-javascript-programmer-know Not jQuery. ...

  8. 移动端html touch事件

    诸如智能手机和平板电脑一类的移动设备通常会有一(capacitive touch-sensitivescreen),以捕捉用户的手指所做的交互.随着移动网络的发展,其能够支持越来越复杂的应用,web开 ...

  9. 为npm设置代理,解决网络问题

    为npm设置代理,解决网络问题 npm config set proxy=http://127.0.0.1:1080

  10. Tomcat启动Web.xml引用其它XML配置报FileNotFound异常解决方案

    如果使用JEECG框架进行Tomcat启动时,如果web.xml引用了其他xml文件,需要在tomcat文件夹里的config文件夹里的context.xml文件里的Context标签里配置xmlBl ...