Fluent API in Code-First [Code-First series], fluentcode-first

Source: Internet
Author: User

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.

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.