Click here to Skip to main content
11,638,881 members (75,365 online)
Click here to Skip to main content

Fluent API vs. Data Annotations Working with Configuration – Part 2

, 14 Nov 2012 CPOL 12.5K 10
Rate this:
Please Sign up or sign in to vote.
Fluent API vs. Data Annotations working with configuration.

I hope, you have already learned very basic on Fluent API and Data Annotations in Part 1. And hope you have learned how to work with code first using Author and Book classes on previous article. We have decided to change the class name from Author to Publisher as Publisher and Book is more appropriate for One to Many relationship. Ok let’s start, Code First allows you to override its conventions by applying additional configurations. You can choose either attribute based data annotation or strongly typed Fluent API for those configurations. You have seen how how to override the OnModelCreating method. The DbModelBuilder that is provided to the OnModelCreating method is the class for adding configurations.

public DbSet Publishers { get; set; }
public DbSet Books { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Publisher>().Property(n => n.PublisherName).IsRequired();
    modelBuilder.Entity<Publisher>().Property(d => d.Description).HasMaxLength(500);
    modelBuilder.Entity<Publisher>().Property(p => p.Photo).HasColumnType("image");
    modelBuilder.Entity<Book>().Property(n => n.BookName).IsRequired();
    modelBuilder.Entity<Book>().Property(n => n.BookName).HasMaxLength(200);
}

So you know how to work with Required, Column Type check, Max Length. How to configure properties of a class. You can also configure the class itself. You can also specify that which database table it should map to:

modelBuilder.Entity<Publisher> ().ToTable(“Your_Desired_Table_Name);

It is possible to use both Data Annotations and Fluent API. If you use both together your code will become inconsistent. It is always advised that try to keep your code consistent. So use any one, either Data Annotations Or Fluent API.

Now, if you need lot of configuration to perform, the OnModelCreating method mighty quickly become overwhelmed with code. You can group configuration by entity within individual EntityTypeConfiguration classes and then call them. Let us separate Book and Publisher. Add two classes: BookConfiguration, PublisherConfiguration. Your classes should look like the following:

public class BookConfiguration : EntityTypeConfiguration<Book>
{
    public BookConfiguration()
    {
        Property(n => n.BookName).IsRequired().HasMaxLength(200);
    }
}
public class PublisherConfiguration : EntityTypeConfiguration<Publisher>
{
    public PublisherConfiguration()
    {
        Property(n => n.PublisherName).IsRequired();
        Property(d => d.Description).HasMaxLength(500);
        Property(p => p.Photo).HasColumnType("image");
    }
}

Calling modelBuilder.Entity<Book>() will actually create an EntityTypeConfiguration<Book> and return it to you, so whichever approach you choose, you are accessing the same API.

Now change your OnModelCreating method that consumes those two classes you created earlier. Your code should look like the following:

public class LibraryDB:DbContext
{
    public DbSet<Publisher> Publishers { get; set; }  
    public DbSet<Book> Books { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new BookConfiguration());
        modelBuilder.Configurations.Add(new PublisherConfiguration());
    }
}

Now you will learn how to recreate the database when the model change or to match the model to database. Before that, make sure you have installed EF latest version properly. You can use NuGet package manager for that. The Entity Framework can create, update, drop databases when application runs. The Entity Framework recommends to use “Database Migrations” as a prevention for loosing old records. But for production environment you always need to generate some test data. Entity Framework has “Seed” method that allows you to seed some dummy data in the database. Let's have a look how to do that:

There are two methods for achieving your purpose , write any one of the following lines within Application_Start inside “Global.asax.cs” file. There is another good way to do that with dummy data. Add a new class named DBInitializer inherits from DropCreateDatabaseIfModelChanges<LibraryDB>.

protected override void Seed(LibraryDB context)
{
    //base.Seed(context);
    context.Publishers.Add(new Publisher
    {
        PublisherName="O'Reilly",
        Description="NA"
    });
    context.Publishers.Add(new Publisher
    {
        PublisherName = "McGraw.Hill",
        Description = "NA"

    });
   context.SaveChanges();
}

Hope you all enjoyed. Have fun Smile | :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Monjurul Habib
Software Developer (Senior)
Bangladesh Bangladesh
A life-long-learner, maker and soft music fan. Likes building things to solve problems. Lives in Dhaka with wife and wonderful, smart kid and works as a Senior Software Engineer in applications architecture team.

Has years of successful records serving mid and large scale .NET applications in domestic and international client environment. Expertise in different areas of software development life cycles and Software Architecture.

Always looks for new information and value feedback (especially where got something wrong!).

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Chaouli Mohamed5-Feb-13 22:52
memberChaouli Mohamed5-Feb-13 22:52 
GeneralMy vote of 5 Pin
Munir Hassan18-Nov-12 7:31
memberMunir Hassan18-Nov-12 7:31 
GeneralRe: My vote of 5 Pin
Monjurul Habib18-Nov-12 19:50
memberMonjurul Habib18-Nov-12 19:50 
GeneralMy vote of 5 Pin
Mahbub_Hasan_Aiub13-Nov-12 21:58
memberMahbub_Hasan_Aiub13-Nov-12 21:58 
GeneralRe: My vote of 5 Pin
Monjurul Habib13-Nov-12 22:05
memberMonjurul Habib13-Nov-12 22:05 
GeneralMy vote of 5 Pin
cisjackie13-Nov-12 21:36
membercisjackie13-Nov-12 21:36 
GeneralRe: My vote of 5 Pin
Monjurul Habib13-Nov-12 21:41
memberMonjurul Habib13-Nov-12 21:41 
GeneralNice post Pin
Shahriar Iqbal Chowdhury13-Nov-12 20:47
memberShahriar Iqbal Chowdhury13-Nov-12 20:47 
GeneralRe: Nice post Pin
Monjurul Habib13-Nov-12 20:52
memberMonjurul Habib13-Nov-12 20:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150728.1 | Last Updated 14 Nov 2012
Article Copyright 2012 by Monjurul Habib
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid