You do not need to create PostTagMap table explicitly. Entity framework will create the join table for you and as you are using Navigation properties (ICollection, etc.). Try addings a List of tags directly to one of your posts in the Seed method. This should then show up in your Index view.
Code:
public class Post
{
public int PostID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public virtual Category Category { get; set; }
public int CategoryID { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
public class Category
{
public int CategoryID { get; set; }
public string Name { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
public class Tag
{
public int TagID { get; set; }
public string Name { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
public class PostDbContext : DbContext
{
public PostDbContext()
{
Database.SetInitializer(new PostDbContextInitializer());
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Post>().HasMany<Tag>(s => s.Tags).WithMany(c => c.Posts).Map(m =>
{
m.MapLeftKey("PostID");
m.MapRightKey("TagID");
m.ToTable("PostTagMap");
});
base.OnModelCreating(modelBuilder);
}
public DbSet<Post> Post { get; set; }
public DbSet<Tag> Tag { get; set; }
public DbSet<Category> Category { get; set; }
}
public class PostDbContextInitializer : DropCreateDatabaseIfModelChanges<PostDbContext>
{
protected override void Seed(PostDbContext context)
{
List<Category> listCategory = new List<Category>
{
new Category { Name = "Programming" },
new Category { Name = "Designing" },
new Category { Name = "Database" },
};
List<Tag> listTag = new List<Tag>
{
new Tag { Name = "Csharp" },
new Tag { Name = "Asp.Net" },
new Tag { Name = "Sencha Touch" },
new Tag { Name = "MVC" },
new Tag { Name = "SqlServer" },
new Tag { Name = "Oracle" },
new Tag { Name = "Bootstrap" },
new Tag { Name = "Jquery" },
};
List<Post> listPost = new List<Post>
{
new Post { Title = "List Paging in Sencha Touch", Description = "In this one I am going to add one more important and most used functionality i.e. paging in sencha touch List.", Category = listCategory.Find(m => m.Name.Equals("Programming")), Tags = listTag.Where(x => x.Name.Equals("Sencha Touch") || x.Name.Equals("Asp.Net")).ToList() },
new Post { Title = "CRUD Operation using Sencha Touch and ASP.Net MVC Web API", Description = "CRUD Operation using Sencha Touch and ASP.Net MVC Web API In this article I am going to explain and demonstrate how to create", Category = listCategory.Find(m => m.Name.Equals("Programming")) },
new Post { Title = "Union Example in SQL Server", Description = "In this article I am going to explain a use of union operator in SQL Server Database with a real life scenario and example. The UNION operator is used to combine the result-set of two or more SELECT statements.", Category = listCategory.Find(m => m.Name.Equals("Database")) },
new Post { Title = "Pivot with Dynamic columns in SQL Server", Description = "Pivot with Dynamic columns in SQL Server In this article I will present how we can write a Dynamic PIVOT.", Category = listCategory.Find(m => m.Name.Equals("Database")) },
};
listCategory.ForEach(m =>
{
context.Category.Add(m);
});
context.SaveChanges();
listTag.ForEach(m =>
{
context.Tag.Add(m);
});
context.SaveChanges();
listPost.ForEach(m =>
{
context.Post.Add(m);
});
context.SaveChanges();
base.Seed(context);
}
}