0

I'm going to make user able to assign several tag for blog post(like what stackoverflow do about tag and question), this is my post model

 public class Post
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Body { get; set; }
        public string Summary { get; set; }
        public DateTime CreationDate { get; set; }
        public string UrlSlug { get; set; }
        public string Picture { get; set; }
        public int TagId { get; set; }
        public virtual Tag Tag { get; set; }
    }

and this is the tag

public class Tag
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime CreationDate { get; set; }
        public string TagSlug { get; set; }

    }

and when I want to create a post I simply get list of all tags in a dropdownlist and get it's Id in post action and blah blah! So to make it possible to assign several tags for a post how should change my model?

1 Answer 1

0

It sounds like you want to establish a Many:Many relationship with your Posts and Tags, since a Post can have many Tags, and a Tag will be applied to many Posts.

That means you'll at least want to store a collection of the associated Tags on your the Post object. Optionally, you may want to store a collection of associated Posts on your Tag object as well.

So, changing your single Tag to a collection:

public class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Body { get; set; }
    public string Summary { get; set; }
    public DateTime CreationDate { get; set; }
    public string UrlSlug { get; set; }
    public string Picture { get; set; }

    // Navigation property
    public virtual ICollection<Tag> Tags { get; set; }
}

public class Tag
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime CreationDate { get; set; }
    public string TagSlug { get; set; }

    // Navigation property (optional)
    public virtual ICollection<Post> Posts { get; set; }
}

If you're doing code first development, Entity Framework should be able to sort out your database structure and tables using the default conventions/mappings.

If you already have an existing database, you may have to perform some explicit mapping, e.g.

 modelBuilder.Entity<Post>()
                .HasMany(t => t.Tags)
                .WithMany(p => p.Posts)
                .Map(m => m.MapLeftKey("PostId")
                           .MapRightKey("TagId")
                           .ToTable("PostTags"));
2
  • thanks for your answer, I've released my web site and now wanna to use code first migration, is it possible to use you solution with EF code first migration?? Aug 11, 2013 at 9:27
  • Hi, although I haven't actually used Code First Migrations, it looks like it should be fairly straightforward. The Code First Migrations section in the Entity Framework documentation has some good tutorials: msdn.microsoft.com/en-us/data/ee712907 (Code First Migrations are about half way down the page).
    – Chris
    Aug 11, 2013 at 11:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.