Fluent API in Code-first "Code-first series"

Source: Internet
Author: User

In the previous chapters, we have seen a variety of different data annotation features. Now let's take a look at the fluent API.

The Fluent API is another way to configure the domain class, which provides more configuration than the data annotation feature.

Mappings"mapping" database "turn into Databases"
Model-wide Mapping"model mapping"
  • Set Default Schema "Setting the schema of the defaults"
  • Set Custom convetions"Customize Convention"
entity Mapping
  • to Single or Multi Ple Tables and Schema "single or multiple data tables, Schama"
  • to Complex type< Span style= "Background-color: #ff6600;" > "complex Type"
  • inheritance hierarchies
Property Mapping"attribute mapping"
  • To column, column Name, column Type, Nullable or not Null Column, column size, Columns Order

Go to column, column name, column type, nullable column or non-nullable column, column size, column order

  • To Concurrency column"Concurrent columns"
  • To Foreign keycolumn "Foreign key Columns"
  • To configure relationships"configuration relationship"

Let's take a look at the code previously written in the context:

UblicclassSchoolcontext:dbcontext { PublicSchooldbcontext ():Base()     {    }     PublicDbset<student> Students {Get;Set; }  PublicDbset<standard> Standards {Get;Set; } protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder) {//Configure domain classes using modelBuilder Here "Configuring the Domain class here"        Base.    Onmodelcreating (ModelBuilder); }}

The use of the Fluent API is configured in this method onmodelcreating. Dbmodelbuider is the primary class for configuring domain classes, in which your domain class is initialized.

Of course you can use both the data annotation feature and the fluent API. The priority given by Code-first is: Fluent api> data Annotations > Default conventions.

The Dbmodelbuilder class contains many important properties and methods to learn more about this class, see MSDN.

In the following section, we begin to learn to configure our domain classes using the Fluent API.

Attached catalogue:

    • What is code First
    • A simple Code First example
    • Code-first conventions
    • DB initialization (database initialization)
    • Inheritance strategy (Inheritance policy)
    • Configure domain Classes (configuration domain Class)
    • DataAnnotations (data annotations)
    • Fluent API
    • Configure one-to-one (Configuring a one-to-one relationship)
    • Configure One-to-many (Configuring a one-to-many relationship)
    • Configure Many-to-many (Configuring many-to-many relationships)
    • Move configurations (data migration)
    • DB initialization strategy (database initialization policy)

Fluent API in Code-first "Code-first series"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.