Techumber
Home Blog Work

Creating Associations In Entity Framework Code First

Published on February 21, 2013

In Previous posts i have wrote about Entity frame work. How to work with it. How to use it to create database. How to configure it. How to organize entity framework configurations. In today’s post i will explain how to create One-to-One,One-to-Many,Many-to-Many relations in Entity frame work code first. Read previous posts part1,part2,part3 Creating Associations In Entity Framework Code First

We are going to create association as shown in below image. Creating Associations In Entity Framework Code First Many-to-Many Association Now we will create many to many association between post and tag Entities. Because Each post may have many tags as well as each tag may have many post. So lets see how to do it.

public class Post
{
public int PostId { get; set; }
public string PostTitle { get; set; }
public string PostBody { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
public class Tag
{
public int TagId { get; set; }
public string TagName { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}

When we run this code it will create a new table TagPosts at run time. One-to-Many Association

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 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; }
}

One-to-One Assocation This is only toughest associations. There are many ways do this association. In order to make it simple I am creating this association as below.

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 class PostMeta
{
public int PostMetaId { get; set; }
public string PostAuthor { get; set; }
}

Thought it is not perfect one-to-one association but it can be used as one. Complete Code

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 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());
}
}
}