Home > dotnet 4, Entity Framework > EF Code First:Tips & Tricks

EF Code First:Tips & Tricks

In this post I am going to show you some small tips & tricks about EF Code First which are quite useful.

Pluralization: By default Entity Framework pluralizes the table name, so if I have a domain object called ‘Employee’ it will create a table ‘Employees’. In order to turn it off you have to remove the Pluralizing table name from the convention collection of the modelbuilder object.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
   modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}

which is by the way is in the following namespace.

using System.Data.Entity.ModelConfiguration.Conventions.Edm.Db;

And the DbSet declaration is like this.

public DbSet<Employee> Employee { get; set; }

Ignoring properties: Suppose you have a property in your domain model which is quite important from the domain perspective but is not stored in the database. For example below is an Employee domain entity which has a property ‘IsQualifiedForTask’ which is computational property and is not stored in the database.

public class Employee
{
   public int Id { get; set; }
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public bool IsQualifiedForTask { get; set; }
}

In order to tell Entity Framework to do not persist this property in the database you could use the ‘Ignore’ keyword in the employee configuration class

public class EmployeeConfiguration : EntityTypeConfiguration<Employee>
{
   public EmployeeConfiguration()
   {
      this.Ignore(x => x.IsQualifiedForTask);
   }
}

Restricting Length: Suppose you want to restrict the size of a string column you can use the ‘HasMaxLength’ setting like this.

public class FacultyConfiguration : EntityTypeConfiguration<Faculty>
{
    public FacultyConfiguration()
    {
        this.Property(x => x.AnnuaLeave).IsRequired();
        this.Property(x => x.FirstName).HasMaxLength(100);
    }
}

Max string size: If you have a string property in your domain object and you don’t specify a size, by default Entity Framework will creates a corresponding column of type nvarchar(4000). This works fine for most of the scenario but what if it’s like a notes or a details field where the string can be more than 4000 characters. For example below is an article class which has a content column like this.

public class Article
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
}

and in the configuration class I will map the ‘Content’ column to max size like this.

public class ArticleConfiguration : EntityTypeConfiguration<Article>
{
    public ArticleConfiguration()
    {
        this.Property(x => x.Content).HasColumnType("nvarchar(max)");
    }
}

That’s all folks, until next time enjoy playing around with Entity Framework Code First.

Advertisements
  1. Bryan
    April 28, 2011 at 7:38 am

    Thanks for the tips , saved a lot of time 😉

  2. Xavier Sossamon
    May 17, 2011 at 2:19 am

    Hey great site I really enjoyed the read thank you for posting such valuable info and keeping up with the latest trends.

    • May 26, 2011 at 9:00 pm

      Thanks Bryan and Xavier, I am glad you like the content of my blog and I’ll try my best to keep my content close to practical and useful programming 🙂

  3. zeldajoseph
    November 2, 2014 at 4:05 pm

    Just desire to say your article is as surprising.
    The clearness in your submit is simply nice and i could
    suppose you’re an expert on this subject. Well along with your permission let me
    to grab your RSS feed to stay up to date with approaching post.
    Thank you a million and please keep up the gratifying work.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: