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, 1:45 AM EDT
Very nice.....
Delpin Susai RajTOP 100Posted Aug 28, 2016, 5:37 AM EDT
Good one
Kuppurasu NagarajTOP 100Posted May 22, 2016, 1:31 AM EDT
Nice Sharing..
Vignesh ManiTOP 100Posted May 21, 2016, 5:27 AM EDT
Nice
Sonu ChaudharyPosted May 19, 2016, 7:32 AM EDT
nice work done
Pankaj Kumar ChoudharyTOP 100Posted May 19, 2016, 7:28 AM EDT
Thanks Bhuvanesh Mohankumar Sir 4 ur Appreciation ...........
Pankaj Kumar ChoudharyTOP 100Posted May 19, 2016, 7:27 AM EDT
Thanks Neeraj Kumar............
Pankaj Kumar ChoudharyTOP 100Posted May 19, 2016, 7:27 AM EDT
Thanks Debasis Saha Sir........
Pankaj Kumar ChoudharyTOP 100Posted May 19, 2016, 7:27 AM EDT
Thanks Anu Vivin Sir.........
Bhuvanesh MohankumarPosted May 19, 2016, 5:25 AM EDT
Your sample coding itself explains the functionality, well explained too Pankaj Kumar Choudhary
Neeraj KumarPosted May 19, 2016, 3:17 AM EDT
Nice Pankaj Kumar Choudhary
Debasis SahaTOP 100Posted May 19, 2016, 2:24 AM EDT
Good One..
Anu VPosted May 19, 2016, 2:16 AM EDT
Nice