In the code first approach of entity framework we have two methods to configure the entities: DataAnnotations and Fluent API.
Generally developers prefer the FluentAPI approach because it provide more flexibility and configuration methods. All configuration codes of domain classes are present in “OnModelCreating” methods. Configuration of every domain class in OnModelCreating can makes it more complex and unmanageable. But, Code First approach provides a method whereby we can place the configuration of each entity into separate files that makes easy to focus on configuration code of a single domain class.
Let us take tthe below example:
Let us take tthe below example:
- namespace ConsApp
- {
- using System;
- using System.Data.Entity;
- using System.ComponentModel.DataAnnotations.Schema;
- using System.Linq;
- public partial class Data_Model : DbContext
- {
- public Data_Model()
- : base("name=Data_Model")
- {
- }
- public virtual DbSet<TblComment_And_Rating> TblComment_And_Rating { get; set; }
- public virtual DbSet<TblCompany_General_Info> TblCompany_General_Info { get; set; }
- public virtual DbSet<TblCompany_Payment_Info> TblCompany_Payment_Info { get; set; }
- protected override void OnModelCreating(DbModelBuilder modelBuilder)
- {
- modelBuilder.Entity<TblComment_And_Rating>()
- .Property(e => e.Comment_Text)
- .IsUnicode(false);
- modelBuilder.Entity<TblComment_And_Rating>().
- Property(e => e.Company_Id)
- .IsConcurrencyToken(true);
- modelBuilder.Entity<TblComment_And_Rating>().
- Property(e => e.Company_Id)
- .HasColumnName("cmp_Id");
- modelBuilder.Entity<TblCompany_General_Info>()
- .Property(e => e.Company_Name)
- .IsUnicode(false);
- modelBuilder.Entity<TblCompany_General_Info>()
- .Property(e => e.Contact_Person)
- .IsUnicode(false);
- modelBuilder.Entity<TblCompany_General_Info>()
- .Property(e => e.Establish_Year)
- .IsFixedLength()
- .IsUnicode(false);
- modelBuilder.Entity<TblCompany_General_Info>()
- .Property(e => e.Pincode)
- .IsFixedLength()
- .IsUnicode(false);
- modelBuilder.Entity<TblCompany_General_Info>()
- .Property(e => e.Address)
- .IsUnicode(false);
- modelBuilder.Entity<TblCompany_General_Info>()
- .Property(e => e.Mobile_Number)
- .IsUnicode(false);
- modelBuilder.Entity<TblCompany_General_Info>()
- .Property(e => e.Landline_Number)
- .IsUnicode(false);
- modelBuilder.Entity<TblCompany_General_Info>()
- .Property(e => e.Email_Id)
- .IsUnicode(false);
- modelBuilder.Entity<TblCompany_General_Info>()
- .Property(e => e.Company_Logo)
- .IsUnicode(false);
- modelBuilder.Entity<TblCompany_General_Info>()
- .Property(e => e.Company_Image)
- .IsUnicode(false);
- modelBuilder.Entity<TblCompany_General_Info>()
- .Property(e => e.Website)
- .IsUnicode(false);
- modelBuilder.Entity<TblCompany_General_Info>()
- .Property(e => e.Keyword)
- .IsUnicode(false);
- modelBuilder.Entity<TblCompany_General_Info>()
- .Property(e => e.Facebook_Id)
- .IsUnicode(false);
- modelBuilder.Entity<TblCompany_General_Info>()
- .Property(e => e.Linkedin_Id)
- .IsUnicode(false);
- modelBuilder.Entity<TblCompany_General_Info>()
- .Property(e => e.Twitter_Id)
- .IsUnicode(false);
- modelBuilder.Entity<TblCompany_General_Info>()
- .Property(e => e.Google_Plus_Id)
- .IsUnicode(false);
- modelBuilder.Entity<TblCompany_General_Info>()
- .HasMany(e => e.TblComment_And_Rating)
- .WithRequired(e => e.TblCompany_General_Info)
- .WillCascadeOnDelete(false);
- modelBuilder.Entity<TblCompany_General_Info>()
- .HasMany(e => e.TblCompany_Payment_Info)
- .WithRequired(e => e.TblCompany_General_Info)
- .WillCascadeOnDelete(false);
- }
- }
- }
Now we create the “General_Info_Config” and “Comment_Rating_Config” configuration classes that derives from EntityTypeConfiguration<TEntity>.
General_Info_Config Class
- using System;
- using System.Collections.Generic;
- using System.Data.Entity.ModelConfiguration;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsApp
- {
- class General_Info_Config: EntityTypeConfiguration<TblCompany_General_Info>
- {
- public General_Info_Config()
- {
- Property(e => e.Company_Name).
- IsUnicode(false);
- Property(e => e.Contact_Person)
- .IsUnicode(false);
- Property(e => e.Establish_Year)
- .IsFixedLength()
- .IsUnicode(false);
- Property(e => e.Pincode)
- .IsFixedLength()
- .IsUnicode(false);
- Property(e => e.Address)
- .IsUnicode(false);
- Property(e => e.Mobile_Number)
- .IsUnicode(false);
- Property(e => e.Landline_Number)
- .IsUnicode(false);
- Property(e => e.Email_Id)
- .IsUnicode(false);
- Property(e => e.Company_Logo)
- .IsUnicode(false);
- Property(e => e.Company_Image)
- .IsUnicode(false);
- Property(e => e.Website)
- .IsUnicode(false);
- Property(e => e.Keyword)
- .IsUnicode(false);
- Property(e => e.Facebook_Id)
- .IsUnicode(false);
- Property(e => e.Linkedin_Id)
- .IsUnicode(false);
- Property(e => e.Twitter_Id)
- .IsUnicode(false);
- Property(e => e.Google_Plus_Id)
- .IsUnicode(false);
- HasMany(e => e.TblComment_And_Rating)
- .WithRequired(e => e.TblCompany_General_Info)
- .WillCascadeOnDelete(false);
- HasMany(e => e.TblCompany_Payment_Info)
- .WithRequired(e => e.TblCompany_General_Info)
- .WillCascadeOnDelete(false);
- }
- }
- }
Comment_Rating_Config Class
- using System.Data.Entity.ModelConfiguration;
- namespace ConsApp
- {
- class Comment_Rating_Config : EntityTypeConfiguration<TblComment_And_Rating>
- {
- public Comment_Rating_Config()
- {
- Property(e => e.Comment_Text)
- .IsUnicode(false);
- Property(e => e.Company_Id)
- .IsConcurrencyToken(true);
- Property(e => e.Company_Id)
- .HasColumnName("cmp_Id");
- }
- }
- }

In the above code we added the references of configurations classes into OnModelCreating method. It is a good approach to separate the configuration of domain classes and create a separate configuration class for each domain classes. Using this approach we can reduce the complexity of code and it reduce the efforts to find out the all configuration methods related to a specific domain classes. So if you are developing a large application that contain many domain classes and you are using “Fluent API” approach for configuration the domain classes, in that case you should be separate the configuration codes related to domain classes.

Ankur VermaPosted Sep 29, 2016, 5:45 AM
Very nice.....
Delpin Susai RajPosted Aug 28, 2016, 9:37 AM
Good one
Kuppurasu NagarajPosted May 22, 2016, 5:31 AM
Nice Sharing..
Vignesh ManiPosted May 21, 2016, 9:27 AM
Nice
Sonu ChaudharyPosted May 19, 2016, 11:32 AM
nice work done
Pankaj Kumar ChoudharyPosted May 19, 2016, 11:28 AM
Thanks Bhuvanesh Mohankumar Sir 4 ur Appreciation ...........
Pankaj Kumar ChoudharyPosted May 19, 2016, 11:27 AM
Thanks Neeraj Kumar............
Pankaj Kumar ChoudharyPosted May 19, 2016, 11:27 AM
Thanks Debasis Saha Sir........
Pankaj Kumar ChoudharyPosted May 19, 2016, 11:27 AM
Thanks Anu Vivin Sir.........
Bhuvanesh MohankumarPosted May 19, 2016, 9:25 AM
Your sample coding itself explains the functionality, well explained too Pankaj Kumar Choudhary
Neeraj KumarPosted May 19, 2016, 7:17 AM
Nice Pankaj Kumar Choudhary
Debasis SahaPosted May 19, 2016, 6:24 AM
Good One..
Anu VPosted May 19, 2016, 6:16 AM
Nice