Beginner Entity Framework Code First-Part3
Published on February 18, 2013
Hi All, Last two post i have worte What is Entity Framework? How to create database using entity Freamework? How to configure entity Framework using DataAnnotation as well as Fluent API. Read previous posts at Part1, Part2.
In today’s post I explain how to organize Fluent API configurations. First of all why we need to organize configurations. Answer is simple. To avoid confusion. In previous post example we configure Post Entity Class as below.
public class BlogEntites : DbContext {
//Some code removes for clarity
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Entity<post>()
.Property(p => p.PostTitle)
.IsRequired();
}
}
In above example we configuring only one property. Let’s Imagine Post class have 10 Properties to configure, Comments class has 15 Properties to configure. What if we need to work on a big e-commerce solutions like nopcommerce. If we wrote all those configurations under onModelCreating function, who dazzle the code would be. How to organize? To Organize the configurations we will group the entity class configurations into EntityTypeConfiguration class. Later we will add all these configurations in the onModelCreating method. Now lets see below example to understand what i’m talking about. Creating Entity type configuration classes Now, Lets add configurations for our Post,Comment classes. Create new class file ‘BlogEFConfig.cs’ and add the classes as below.
public class postConfig : EntityTypeConfiguration<Post> {
public postConfig() {
Property(p => p.PostTitle).IsRequired().HasMaxLength(256);
Property(p => p.PostBody).IsRequired();
}
}
public class commentConfig : EntityTypeConfiguration<Comment> {
public commentConfig() {
Property(c => c.CommentAuthor).IsRequired();
Property(c => c.CommentBody).IsRequired();
}
}
Adding Configurations We created configurations in previous step now it’s time to add these configurations in OnModelCreating method. Replace OnModelCreatin method as below.
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Configurations.Add(new postConfig());
modelBuilder.Configurations.Add(new commentConfig());
}
Overvidw BlogEFConnfigs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity.ModelConfiguration;
namespace EFDBCreation {
public class postConfig : EntityTypeConfiguration<Post> {
public postConfig() {
Property(p => p.PostTitle).IsRequired().HasMaxLength(256);
Property(p => p.PostBody).IsRequired();
}
}
public class commentConfig : EntityTypeConfiguration<Comment> {
public commentConfig() {
Property(c => c.CommentAuthor).IsRequired();
Property(c => c.CommentBody).IsRequired();
}
}
}
BlogEntites.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace EFDBCreation
{
public class Post
{
public int PostId { get; set; }
public string PostTitle { get; set; }
public string PostBody { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
public class Comment
{
public int CommentId { get; set; }
public string CommentAuthor { get; set; }
public string CommentBody { get; set; }
public virtual Post Post { get; set; }
}
public class BlogEntites : DbContext
{
public DbSet<Post> Posts { get; set; }
public DbSet<Comment> Comments { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new postConfig());
modelBuilder.Configurations.Add(new commentConfig());
}
}
public class BlogDbInitilizer : DropCreateDatabaseIfModelChanges<BlogEntites>
{
protected override void Seed(BlogEntites context)
{
var seedpost = new Post
{
PostTitle = "Seed Post Title 1",
PostBody = "Seed post Body 1",
Comments = new List<Comment>
{
new Comment{CommentAuthor="aravind",CommentBody="my first comment on seed post 1"},
new Comment{CommentAuthor="dimpu",CommentBody="my second Commment on seed post1"}
}
};
context.Posts.Add(seedpost);
context.SaveChanges();
}
}
}