Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3949692
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:31:25+00:00 2026-05-20T01:31:25+00:00

I really need someone to help me to fully understand how to do many-to-many

  • 0

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:

  1. How to config my model to indicates
    some tables are many-to-many.
  2. How to properly do insert.
  3. 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:

  1. What to do on the
    EntityTypeConfiguration to set the
    relationship between them as well as
    mapping to an table/object that
    associates with the many-to-many
    relationship.
  2. How to insert a new music sheets
    (where it might have multiple
    authors or multiple tags).
  3. 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.

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:31:26+00:00Added an answer on May 20, 2026 at 1:31 am
    1. In the EF4 CTP5 the relationship is done by default convention when you put public virtual ICollection in each of the classes of the many to many relationship, as you already have done, your context class should look like this:

      public class YourContextName : DbContext
      {
          public DbSet<MusicSheet> MusicSheets { get; set; }
          public DbSet<Tag> Tags { get; set; }
          public DbSet<Author> Authors { get; set; }
      }
      
    2. Very simple you just create a instance of the MusicSheet class and then add all the instances of you authors and tags to each of the collections of Authors and Tags in your MusicSheet, and then add your instance of MusicSheet to your context collection of MusicSheets and then call SaveChanges:

              MusicSheet musicSheet = new MusicSheet
                                      {
                                          Title = "Music Sheet 1",
                                          Key = "Key",
                                          Authors = new List<Author>
                                                        {
                                                            new Author
                                                                {
                                                                    Name = "Author 1",
                                                                    Bio = "Author 1 biographic text..."
                                                                },
                                                            new Author
                                                                {
                                                                    Name = "Author 2",
                                                                    Bio = "Author 2 biographic text..."
                                                                }
                                                        },
      
                                          Tags = new List<Tag>
                                                     {
                                                         new Tag {TagName = "TagA"},
                                                         new Tag {TagName = "TagC"}
                                                     }
                                      };
      
      
          var context = new YourContextName();
          context.MusicSheets.Add(musicSheet);
          context.SaveChanges();
      
    3. To update you have to load your MusicSheet and remove the tags you don’t want and then add the ones you need to add, this is how:

          var context = new YourContextName();
          var myMusicSheet = context.MusicSheets.First();
      
          //The Tag you wnat to remove.
          var tagToRemove = myMusicSheet.Tags.First();
      
          var tagToAdd = new Tag {TagName = "TagX"};
      
          myMusicSheet.Tags.Remove(tagToRemove);
          myMusicSheet.Tags.Add(tagToAdd);
      
          context.Entry(myMusicSheet).State = EntityState.Modified;
          context.SaveChanges();
      

    You can also find any author and/or tag that you know that exist and added to your MusicSheet and vice versa, but this is the foundation.

    Remember this is for the EF4 CTP5 Code first…

    Excuse me my English is not my main language, I hope this can help you, best regards from Dominican Republic.

    PS: Don’t forget to add references to EntityFramework and System.Data.Entity, is your responsibility to do anything else like unit test, validation, exception handling…etc

    EDIT:

    First you need to add a constructor to your models:

    public class Tag
    {
        [Key]
        public int ID { get; set; }
        public string TagName { get; set; }
    
        public Tag()
        {
           MusicSheets = new List<MusicSheet>();
        }        
    
        public virtual ICollection<MusicSheet> MusicSheets { get; set; }
    }
    

    …Then you can do something like this:

    var context = new YourContextName();
    var newMusicSheet = new MusicSheet();
        newMusicSheet.Title = "Newly added Music Sheet";
    
    //Your existing Tag.
    var existingTag = contex.Tags.Find(3);        
    
    existingTag.MusicSheets.Add(existingTag);
    
    context.Entry(existingTag).State = EntityState.Modified;
    context.SaveChanges();
    

    You can do the same for all your models.

    I hope this can help you!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i m new to solr so i really need someone to help me understand
Can someone help me to understand how I need to configure buildroot, so that
I really need the following in magento. When a customer shows the config product
I really need help with this last part of my program. I need to
I Really need help in this ... I'm building an application where I need
I really need help this time - Joomla 2.5 and latest K2. I'm using
I really need help on this one. I am having a simple login form
I'm a c# noob but I really need a professional's help. I am using
I am a WPF noob, and really need someone to point me to the
Again, I need someone's help. I am following the below tutorial for SQLite connection

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.