Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity framework 4 many-to-many insertion?
    primarykey
    data
    text
    <p>I'm not very familiar with the many-to-many insertion process using Entity Framework 4, POCO. I have a blog with 3 tables: Post, Comment, and Tag. A <strong>Post</strong> can have many <strong>Tags</strong> and a <strong>Tag</strong> can be in many <strong>Posts</strong>. Here are the <strong>Post</strong> and <strong>Tag</strong> models:</p> <pre><code>public class Tag { public int Id { get; set; } [Required] [StringLength(25, ErrorMessage = "Tag name can't exceed 25 characters.")] public string Name { get; set; } public virtual ICollection&lt;Post&gt; Posts { get; set; } } public class Post { public int Id { get; set; } [Required] [StringLength(512, ErrorMessage = "Title can't exceed 512 characters")] public string Title { get; set; } [Required] [AllowHtml] public string Content { get; set; } public string FriendlyUrl { get; set; } public DateTime PostedDate { get; set; } public bool IsActive { get; set; } public virtual ICollection&lt;Comment&gt; Comments { get; set; } public virtual ICollection&lt;Tag&gt; Tags { get; set; } } </code></pre> <p>Now when I'm adding a new post, I'm not sure what would be the right way to do. I'm thinking that I'll have a textbox where I can select multiple tags for that post (this part is already done), in my controller, I will check to see if the tag is already exists or not, if not, then I will insert the new tag. But I'm not even sure based on the models that I've created for EF, will they create a PostsTags table, or they are creating just a Tags and a Posts table and links between the two?</p> <p>How would I insert the new <strong>Post</strong> and set the tags to that post? Is it just <code>newPost.Tags = Tags</code> (where Tags are the one that got selected, do I even need to check to see if they already exists?), and then something like <code>_post.Add(newPost);</code>?</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload