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.
- Data Annotations (dataannotations)
- 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