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