Entity Framework 4 CTP 5 POCO - Many-to-many configuration, insertion, and update?
        Posted  
        
            by 
                Saxman
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Saxman
        
        
        
        Published on 2011-02-11T23:15:44Z
        Indexed on 
            2011/02/11
            23:25 UTC
        
        
        Read the original article
        Hit count: 381
        
I really need someone to help me to fully understand how to do many-to-many relationship with Entity Framework 4 CTP 5, POCO. I need to understand 3 concepts:
- How to config my model to indicates some tables are many-to-many.
 - How to properly do insert.
 - How to properly do update.
 
Here are my current models:
public class MusicSheet
{
    [Key]
    public int ID { get; set; }
    public string Title { get; set; }
    public string Key { get; set; }
    public virtual ICollection<Author> Authors { get; set; }
    public virtual ICollection<Tag> Tags { get; set; }
}
public class Author
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public string Bio { get; set; }
    public virtual ICollection<MusicSheet> MusicSheets { get; set; }
}
public class Tag
{
    [Key]
    public int ID { get; set; }
    public string TagName { get; set; }
    public virtual ICollection<MusicSheet> MusicSheets { get; set; }
}
As you can see, the MusicSheet can have many Authors or Tags, and an Author or Tag can have multiple MusicSheets.
Again, my questions are:
- What to do on the
EntityTypeConfigurationto set the relationship between them as well as mapping to an table/object that associates with the many-to-many relationship. - How to insert a new music sheets (where it might have multiple authors or multiple tags).
 - How to update a music sheet. For example, I might set TagA, TagB to MusicSheet1, but later I need to change the tags to TagA and TagC. It seems like I need to first check to see if the tags already exists, if not, insert the new tag and then associate it with the music sheet (so that I doesn't re-insert TagA?). Or this is something already handled by the framework?
 
Thank you very much. I really hope to fully understand it rather than just doing it without fully understand what's going on. Especially on #3.
© Stack Overflow or respective owner