ASP.NET MVC Many-to-Many Relationship Using My Own Table

I am new to Code First approach with entity structure, and I know that I have many, many relationships like the entities below, EF will automatically create a table of intermediates:

class Post {
  ...
  public virtual ICollection<Category> Categories {get; set;}
  ... 
}

class Category {
   ...
   public virtual ICollection<Post> Posts {get; set;}
   ...
}

      

However, if I need to have additional data fields in the staging table, one of the possible ways (which I like right now, maybe because I don't know about better ways) would be to define a new Entity of my own, for example:

class Posts_Categories {
   public int Id {get; set;}
   public int CategoryId {get; set;}
   public int PostId {get; set;}
   public string Exrtafield1 {get; set;}
   public int ex extraField2 {get; set;}
   ...
   public virtual Post Post {get; set;}
   public virtual Category Category {get; set;}
}

      

Using this approach, EF creates my custom intermediate table, but it also creates another of its own called "PostsCategories" which contains only a foreign key for Post_Id and another for Category_Id.

How can I make it not create an extra one and use the one I defined? Is this a good way to manage Many to Many relationships with additional data fields?

+3


source to share


3 answers


you should use one-to-many relation like this:



public class Post
{
    public System.Int32 PostId { get; set; }

    [InverseProperty("Post")]
    public virtual ICollection<Posts_Category> PostCategories { get; set; }
}

public class Category
{
    public System.Int32 CategoryId { get; set; }

    [InverseProperty("Category")]
    public virtual ICollection<Posts_Category> PostCategories { get; set; }
}

public class Posts_Category
{
    public System.Int32 PostId { get; set; }

    public System.Int32 CategoryId { get; set; }

    [ForeignKey("PostId")]
    [InverseProperty("PostCategories")]
    public virtual Post Post { get; set; }

    [ForeignKey("CategoryId")]
    [InverseProperty("PostCategories")]
    public virtual Category Category { get; set; }
}

      

+5


source


I needed to expand on Iraj's answer a bit in order for it to work. Another modification is that I include by default ApplicationUser

as one of my tables.

So the ratio ApplicationUser

1- & infin; IdeaVote

& infin; -1 Idea

(i.e. there are users and ideas, users can vote on ideas, and each vote is represented by a link between ApplicationUser

and Idea

.



public class Idea
{
    [Key]
    public int Id { get; set; }

    // This is an ordinary data field
    public string Text { get; set; }

    [InverseProperty("Idea")]
    public virtual ICollection<IdeaVote> Votes { get; set; }
}

public class IdeaVote
{
    [Key]
    public int Id { get; set; }

    public int IdeaId { get; set; }
    [ForeignKey("IdeaId")]
    [InverseProperty("Votes")]
    public virtual Idea Idea { get; set; }

    public string UserId { get; set; }
    [ForeignKey("UserId")]
    [InverseProperty("Votes")]
    public virtual ApplicationUser User { get; set; }
}

      

public class ApplicationUser : IdentityUser
{
    [InverseProperty("User")]
    public virtual ICollection<IdeaVote> Votes { get; set; }

    // Default stuff
}

      

+1


source


It's okay to create this PostsCategories table for the relationship between them, and you will want it. If c is a category you should be able to do things like c.Posts

Usually you would not create your own table manually for this. What data would you store in the "additional" fields? I would probably move the fields to one of the other tables and discard them. Most many relationship tables do not have additional fields.

0


source







All Articles