Fluent API in Code-First [Code-First series], fluentcode-first
In the previous sections, we have seen various data annotation features. Now let's take a look at the Fluent API.
Fluent API is another method of configuration field. It provides more configuration features than data annotation.
| Mappings [ing] |
To Database [convert To Database] |
| Model-wide Mapping [Model ing] |
- Set default Schema [Set the default Schema]
- Set M Convetions [Custom Conventions]
|
| Entity Mapping [Entity ing] |
- To Single or Multiple Tables and Schema [Single or Multiple data Tables, Schama]
- To Complex type [Complex type]
- Inheritance Hierarchies [Inheritance level]
|
| Property Mapping [Property ing] |
- To Column, Column Name, Column Type, Nullable or Not Null Column, Column size, Columns Order
Convert to column, column name, column type, can be empty or not empty column, column size, column order
- To Concurrency column [concurrent column]
- To Foreign key column [Foreign key column]
- To configure relationships [configuration relationship]
|
Let's take a look at the Code previously written in the context:
Ublic 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 [Configure domain classes here] base. onModelCreating (modelBuilder );}}
The Fluent API is configured in OnModelCreating. DbModelBuider is the main class for configuring domain classes. In this method, your domain classes are initialized.
Of course, you can use both the data Annotation Feature and Fluent API. The priority given by Code-First is: Fluent API> data annotation> default convention.
The DBModelBuilder class contains many important attributes and methods.