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共享了很多像我们这样的奇 ...
随机推荐
- 在Linux中CSV转换成XLSX
在linux中,把csv文件转换成excel表格(xlsx或者xls) $ echo -e 'surname,name,age\nCarlo,Smith,23\nJohn,Doe,46\nJane,D ...
- drupal8 用户指南
一句话概括 - 官方文档 概念- Drupal是个内容管理系统哦 那么,什么是内容管理系统? 就是用户自己编辑自己的网站内容的一个系统. 那么,什么是Drupal呢? Drupal是一个通过模块和主题 ...
- Buffer.from(str[, encoding])
Buffer.from(str[, encoding]) Node.js FS模块方法速查 str {String} 需要编码的字符串 encoding {String} 编码时用到,默认:'utf8 ...
- Python之函数作业
Python之函数作业 爬页面 #爬虫页面,send一次爬一次 from urllib.request import urlopen def get(): while True: url = yiel ...
- Spider-scrapy 中的 xpath 语法与调试
把setting中的机器人过滤设为False ROBOTSTXT_OBEY = False 1 语法 artcile 选取所有子节点 /article 选取根元素 artile article/a 选 ...
- 【03】Html书写规范
[03] Html书写规范 1.推荐使用html5的文档声明 <!DOCTYPE HTML> 2.必须申明文档的编码charset,且与文件本身编码保持一致,推荐使用UTF-8编码 ...
- java的动态代理机制详解-----https://www.cnblogs.com/xiaoluo501395377/p/3383130.html
java的动态代理机制详解-----https://www.cnblogs.com/xiaoluo501395377/p/3383130.html
- 九度oj 题目1525:子串逆序打印
题目1525:子串逆序打印 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:3124 解决:530 题目描述: 小明手中有很多字符串卡片,每个字符串中都包含有多个连续的空格,而且这些卡片在 ...
- [luoguP1981] 表达式求值(U•ェ•*U)
传送门 弄两个栈,一个存数,一个存运算符,然后乱搞. 代码 #include <cstdio> #include <cstring> #include <iostream ...
- iLBC 压缩比
*---------------------------------------------------* * ...