3-Tire Application Development Using Entity Framework,ASP.NET Part-I
Published on February 24, 2013
Introduction Three Tier Design fundamentally a most famous application development process, which can be divided into three main levels. First level we will deal with all User Interface(UI). Second level is the business Logic level, where we will apply business rules. Finally third level is Data Access Layer. In this level we implement code required to intact with database and do manipulation on database.
This Kind of development is also known as Component-Based Programming. Today will create a tree tier application in asp.net and we will use Microsoft Entity Framework for our data access layer implementation. I am using Visual studio 2012 web express for this tutorial you can use either 2010 or 2012 visual studio IDE. You can download and use Visual studio 2012 web Express for free. Preparing solution Open your visual studio and create Empty solution, name it as Bloggy(Here I’m creating simple blogging system). To create empty solution in visual studio go File > New project > Other Project Types > Visual Studio Solutions. Select Blank Solution. Add new ASP.NET Web Forms Application to it and name the web form application as Bloggy.UI Add Two class library projects to Bloggy Solution and name them as Bloggy.DL and Bloggy.BL In side the Bloggy.Data project create or rename default class1.cs file into Bloggy.cs. Do the same for Bloggy.BL Project also. Now add one more final Class Library project to the solution name it as Bloggy.Model We don’t use it as layer instead in this we will use this to create our entity models. Adding References Now its time to add reference 1)In Bloggy.DL add reference Bloggy.Model. 2)In Bloggy.BL add reference Bloggy.DL and Bloggy.Model 3)In Bloggy.UI add reference Bloggy.BL and Bloggy.Model 4)In Bloggy.Model add EntityFramework.dll reference(You can download it form Nuget package manager read //www.techumber.com/2013/02/beginner-entity-framework-code-first-part1.html how to do it.) If you observe we have referred Bloggy.Model in all projects. Because we need to use class models in all other projects. Creating Models In my previous post of Beginner Guide to Entity Framework I have explained about entity Framework with Blog Entities. Now we will take those examples here. You can found the previous post post1,post2,post3 and part4. Bloggy.Model > BloggyEntites.cs
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bloggy.Model
{
public class Post
{
public int PostId { get; set; }
public string PostTitle { get; set; }
public string PostBody { get; set; }
public virtual PostMeta PostMeta { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
public class PostMeta
{
public int PostMetaId { get; set; }
public string PostAuthor { get; set; }
}
public class Comment
{
public int CommentId { get; set; }
public string CommentAuthor { get; set; }
public string CommentBody { get; set; }
public int PostId { get; set; }
public virtual Post Post { get; set; }
}
public class Tag
{
public int TagId { get; set; }
public string TagName { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
public class BlogEntites : DbContext
{
public DbSet<Post> Posts { get; set; }
public DbSet<Comment> Comments { get; set; }
public DbSet<PostMeta> PostMetas { get; set; }
public DbSet<Tag> Tags { 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 postseeds = new List<Post>
{
new Post{PostTitle = "Post Title 1",PostBody = "Seed
post Body 1"},
new Post{PostTitle = "Post Title 2",PostBody = "Seed
post Body 2"},
new Post{PostTitle = "Post Title 3",PostBody = "Seed
post Body 3"},
new Post{PostTitle = "Post Title 4",PostBody = "Seed
post Body 4"},
new Post{PostTitle = "Post Title 5",PostBody = "Seed
post Body 5"},
new Post{PostTitle = "Post Title 6",PostBody = "Seed
post Body 6"},
new Post{PostTitle = "Post Title 7",PostBody = "Seed
post Body 7"},
new Post{PostTitle = "Post Title 8",PostBody = "Seed
post Body 8"}
};
foreach(var p in postseeds)
context.Posts.Add(p);
context.SaveChanges();
}
}
}
Bloggy.Model > BloggyModelConfigs.cs
using System;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bloggy.Model
{
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();
}
}
}
It really a big post so i’m braking up into two parts. In next post will explain how to create Data Access Layer, Business Logic Layer and UI Layer.