Entity Framework Code-first (+): Move configurations

Source: Internet
Author: User

Move configurations to separate Class in Code-first:

By now, we had configured all of the domain classes in Onmodelcreating method in the previous sections. When you had a large number of domain classes, then configuring every class in Onmodelcreating can become unmanageable. Code-first enables you to move all the configurations related to one domain class to a separate class.

In the below example, we configured Student entity.

 Public classSchooldbcontext:dbcontext { PublicSchooldbcontext ():Base()     {    }     PublicDbset<student> Students {Get;Set; }  PublicDbset<standard> Standards {Get;Set; }  PublicDbset<studentaddress> studentaddress {Get;Set; } protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder) {modelbuilder.entity<Student> (). ToTable ("Studentinfo"); Modelbuilder.entity<Student> (). haskey<int> (s =S.studentkey); Modelbuilder.entity<Student>()                    . Property (P=P.dateofbirth). Hascolumnname ("DoB")                    . Hascolumnorder (3)                    . Hascolumntype ("datetime2"); Modelbuilder.entity<Student>()                    . Property (P=p.studentname). Hasmaxlength ( -); Modelbuilder.entity<Student>()                    . Property (P=p.studentname).                            Isconcurrencytoken (); Modelbuilder.entity<Student>()                . Hasmany<Course> (s =s.courses). Withmany (c=c.students). Map (CS={cs. Mapleftkey ("StudentID"); Cs. Maprightkey ("CourseID"); Cs. ToTable ("Studentcourse");    }); }}

Now, you can move all the configurations related to Student entity to a separate class which derives from EntityTypeConfiguration<TEntity> . Consider the following Studententityconfigurations class.

 Public classStudententityconfiguration:entitytypeconfiguration<student>{     Publicstudententityconfiguration () { This. ToTable ("Studentinfo");  This. haskey<int> (s =S.studentkey);  This. Property (P =P.dateofbirth). Hascolumnname ("DoB")                    . Hascolumnorder (3)                    . Hascolumntype ("datetime2");  This. Property (P =p.studentname). Hasmaxlength ( -);  This. Property (P =p.studentname).                            Isconcurrencytoken ();  This. Hasmany<course> (s = =s.courses). Withmany (c=c.students). Map (CS={cs. Mapleftkey ("StudentID"); Cs. Maprightkey ("CourseID"); Cs. ToTable ("Studentcourse");    }); }}

As can see above, we had moved all the configuration for the Student entity into constructor of Studententityconfigur ation, which is derived from EntityTypeConfiguration<Student> . You need to specify entity type ' in a generic place holder for which ' include configurations, Student in this case.

Now, your can inform Fluent API about this class, as shown below.

 Public classSchooldbcontext:dbcontext { PublicSchooldbcontext ():Base()     {    }     PublicDbset<student> Students {Get;Set; }  PublicDbset<standard> Standards {Get;Set; }  PublicDbset<studentaddress> studentaddress {Get;Set; } protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder) {//Moved all Student related configuration to Studententityconfiguration classMODELBUILDER.CONFIGURATIONS.ADD (Newstudententityconfiguration ()); }}

Thus, you can use a separate class to configure a domain class to increase the readability and maintainability.

Entity Framework Code-first (+): Move configurations

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.