title: Migrating One-to-Many to Many-to-Many with the new EF Core 5.0
body: <p>I have an ASP.NET Core 5 project (migrated from ASP.NET Core 3.1).
  The project uses EF Core.</p>
  <p>EF Core 5 has built-in support for Many-to-Many. The main reason I first migrated from 3.1 to 5.0.</p>
  <p>Now my project has a One-to-Many relationship, which I want to convert to a Many-to-Many.</p>
  <p>Is such a relationship migration possible?</p>
  <p>Current code:</p>
  <pre><code>    public class File
  {
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      [Key]
      public int Id { get; set; }
  
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public Guid UniqueId { get; set; }
  
      public int RecordId { get; set; }
  
      public virtual Record Record { get; set; }
      // ...
  }
  
  
  public class Record
  {
      [Key]
      public int Id { get; set; }
  
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public Guid UniqueId { get; set; }
  
      public virtual ICollection&lt;File&gt; Files { get; set; }
  }
  
  
    public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;
    {
  
      public DbSet&lt;File&gt; Files { get; set; }
      public DbSet&lt;Record&gt; Records { get; set; }
  
       protected override void OnModelCreating(ModelBuilder builder)
       {
           base.OnModelCreating(builder);
  
           builder.Entity&lt;Record&gt;().ToTable(&quot;Record&quot;);
  
           // for creating GUIDs   
           builder.Entity&lt;Record&gt;().Property(x =&gt; x.UniqueId).HasDefaultValueSql(&quot;newid()&quot;);
           builder.Entity&lt;File&gt;().Property(x =&gt; x.UniqueId).HasDefaultValueSql(&quot;newid()&quot;);
       }
  
    }
  </code></pre>
  <p>In the code above you can see that a Record has zero, one or multiple Files. A File has only one record associated.</p>
  <p>I would like to change this so that a single File can be assigned to multiple Records (this way I reduce duplicate Files and reduce disk space, improve network traffic by increased cache hits etc...).</p>
  <p>In the File-class do I simply remove these lines of code:</p>
  <pre><code>public int RecordId { get; set; }
  
  public virtual Record Record { get; set; }
  </code></pre>
  <p>and replace it with this line below?</p>
  <pre><code>public virtual ICollection&lt;Record&gt; Records { get; set; }
  </code></pre>
  <p>I wouldn't want to destroy and manually change many thousand file record relationships... or breaking them.</p>