EntityFramework Code-first Easy Tutorial (v) configuration of-------domain classes

Source: Internet
Author: User

Preface: In the previous chapter, the domain class is always translated into a model class, because I understand it is a realistic object of the abstract model, do not know right. To prevent possible future ambiguity, this begins with a direct literal translation of domain class.

Before we learned the default Code-first conventions, Code-first used the default conventions to build a conceptual model based on domain classes, code-first using a design model reference as a convention to override the configuration, This means that we can configure domain classes to override these conventions to provide the information that EF needs, and there are two ways to configure the Realm class.

    1. Data Annotations (dataannotations)
    2. Fluent API
Data Note (dataannotation):

Data annotations are a simple configuration-based feature that we can apply to the properties of a domain class or domain class, most of which are under the System.ComponentModel.DataAnnotations namespace, but Data annotations provide only a subset of the Fluent API configuration, which means that if we cannot find a feature in the data model, we must configure it using the Fluent API.

The following example shows the Sudent class using the data annotations:

[Table ("Studentinfo")] Public classstudent{ PublicStudent () {} [Key] Public intSID {Get;Set; } [Column ("Name", typename="ntext")] [MaxLength ( -)]     Public stringStudentname {Get;Set; } [Notmapped] Public int? Age {Get;Set; }  Public intStdId {Get;Set; } [ForeignKey ("StdId")]     Public VirtualStandard Standard {Get;Set; }}

Fluent API:

The Fluent API configuration is an application of the EF build model from the domain class, and we can inject the configuration by covering the Onmodelcreating method of the DbContext class, as shown in the following code:

 Public classSchooldbcontext:dbcontext { PublicSchooldbcontext ():Base("schooldbconnectionstring")     {    }     PublicDbset<student> Students {Get;Set; }  PublicDbset<standard> Standards {Get;Set; }  PublicDbset<studentaddress> studentaddress {Get;Set; } protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder) {//Configure domain classes using Fluent API here        Base.    Onmodelcreating (ModelBuilder); }}   

We can also use an instance object of the Dbmodelbuilder class to configure the Realm class

Later in this article we will detail how the data annotations and the fluent API are used.

EntityFramework Code-first Easy Tutorial (v) configuration of-------domain classes

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.