Entity Framework Code-First(10):Fluent API
2016-07-05 16:47
441 查看
Fluent API in Code-First:
We have seen different DataAnnotations attributes in the previous sections to override default Code-First Conventions. Here, we will learn about Fluent API.Fluent API is another way to configure your domain classes. Fluent API provides more functionality for configuration than DataAnnotations. Fluent API supports the following types of mappings.
Mappings | To Database |
---|---|
Model-wide Mapping | Set default Schema Set Custom Convetions |
Entity Mapping | To Single or Multiple Tables and Schema To Complex type Inheritance Hierarchies |
Property Mapping | To Column, Column Name, Column Type, Nullable or Not Null Column, Column size, Columns Order To Concurrency column To Foreign key column To configure relationships |
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 domain classes using modelBuilder here base.OnModelCreating(modelBuilder); } }
Now, all your configuration code using Fluent API should be in OnModelCreating method. DbModelBuilder is a main class on which you can configure all your domain classes because at this point, all your domain classes would have initialized.
You can also use DataAnnotation and Fluent API at the same time. Code-First gives precedence to Fluent API > data annotations > default conventions.
DbModelBuilder class includes important properties and methods to configure. Visit MSDN for more information on DbModelBulder class.
Let's start to configure entities using Fluent API in the next section.
相关文章推荐
- UITableView 或 UIScrollView 点击状态栏列表回到顶部
- UE4——HelloWorld
- leetcode_Count Numbers with Unique Digits
- 在webapi中使用request.Form
- Entity Framework Code-First(9.4):DataAnnotations - Required Attribute
- iOS开发:keychain存储UUID
- java 基础:Blocking Queues的源码实现
- easyui学习(1)
- 阿里面经之解答 by cmershen(3)——String/StringBuffer/StringBuilder,Java序列化,线程安全,线程同步,ThreadLocal
- UI碎片控件之Fragment——底部导航栏的实现(方法2)
- Number Sequence(poj 1019)
- DelayQueue
- UUID和UDID
- 关于tensorflow 的数据读取线程管理QueueRunner
- PriorityQueue
- 数据库主键用 int自增序列 还是 GUID
- HTML标签marquee实现滚动效果
- Xcode莫名其妙问题could not build module 'Foundation'
- String StringBuffer StringBuilder区别(Java面试)
- UGUI解决嵌套使用多个ScrollRect时的Drag拖动冲突问题