title: How modelbuilder works
body: <p>i have a question about relations between entities created in Code-First:</p>
  
  <p>I have Models:</p>
  
  <pre><code>public class ProjectGroup
  {
      [Key]
      public int ProjectGroupID { get; set; }
      public string ProjectGroupName { get; set; }
  
      //FK
      public virtual ICollection&lt;File&gt; Files { get; set; }
      public virtual ICollection&lt;List&gt; Lists { get; set; }
  }
  
  
  public class File
  {
      [Key]
      public int FileID { get; set; }
      public int ProjectGroupID { get; set; }
      [Required]
      [Display(Name="Ścieżka pliku")]
      public string FilePath { get; set; }
      [Required]
      [Display(Name="Data Zapisu")]
      [DataType(DataType.DateTime)]
      [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd hh:mm:ss}", ApplyFormatInEditMode = true)]
      public DateTime FileSaveDate { get; set; }//no NULL=&gt;error
      [Display(Name="Suma MD5")]
      [StringLength(32)]
      public string FileMD5Hash { get; set; }
      public string IPHost { get; set; }
      public int FileTemplateID { get; set; }
  
      [ForeignKey("ProjectGroupID")]
      public virtual ProjectGroup ProjectGroup { get; set; }
      [ForeignKey("FileTemplateID")]
      public virtual FileTemplate FileTemplate { get; set; }
  
      public virtual ICollection&lt;List&gt; Lists { get; set; }//klucz obcy dla listy
  
  }
  
  public class List
  {
      [Key]
      public int ListID { get; set; }
      public int UserID { get; set; }
      public int ProjectGroupID { get; set; }
      public int FileID { get; set; }
      public bool Modified { get; set; }
      public bool Verified { get; set; }
      public bool Alive { get; set; }
  
      [ForeignKey("UserID")]
      public virtual User User { get; set; }  //referencja,przekazanie nazwy FK
      [ForeignKey("ProjectGroupID")]
      public virtual ProjectGroup ProjectGroup { get; set; }
      [ForeignKey("FileID")]
      public virtual File File { get; set; }
  }
  </code></pre>
  
  <p>And Context:</p>
  
  <pre><code>public class AWZDContext : DbContext
  {
      public AWZDContext()
      {
  
      }
  
      public DbSet&lt;User&gt; Users { get; set; }
      public DbSet&lt;File&gt; Files { get; set; }
      public DbSet&lt;List&gt; Lists { get; set; }
      public DbSet&lt;RemotePC&gt; RemotePCs { get; set; }
      public DbSet&lt;UserType&gt; UserTypes { get; set; }
      public DbSet&lt;ProjectGroup&gt; ProjectGroups { get; set; }
      public DbSet&lt;FileTemplate&gt; FileTemplates { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          base.OnModelCreating(modelBuilder);
  
          modelBuilder.Entity&lt;File&gt;()                
              .HasRequired(f=&gt;f.ProjectGroup)
              .WithMany(t=&gt;t.Files)
              .WillCascadeOnDelete(false);
  
          modelBuilder.Entity&lt;List&gt;()
              .HasRequired(c =&gt; c.ProjectGroup)
              .WithMany(d=&gt;d.Lists)
              .WillCascadeOnDelete(false);
  
          modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;();
      }
  
  }
  </code></pre>
  
  <p>when I created models, I got the following errors: problem with multi-cascade delete. I added modelbuilder in on model creating.
  First I added no parameter in <code>WithMany()</code> and it doubled relations in database <a href="http://s23.postimg.org/rn0la4wl7/Przechwytywanie.png" rel="nofollow">like this</a> </p>
  
  <p>This created many double relations between List and ProjectGroup (File, the same, read below).
  when changed to WithMany(d=>d.Lists) relations looks ok, made only once like between File and ProjectGroup.</p>
  
  <p>Does modelBuilder double the effect of [foreignKey] in model?
  Can anyone explain how this works? Why did it double relation earlier, with no parameter in WithMany()</p>