title: How do I set onUpdate for a foreign key from the Fluent API?
body: <p>I'm trying to get a foreign key relationship in Entity Framework 7 to apply both <code>ON DELETE CASCADE</code> and <code>ON UPDATE CASCADE</code> on the sql constraint.</p>
  
  <p>When using the model builder in <code>OnModelCreating</code> I'm able to specify the relationship to use cascade on deletion. It's finding out where to specify the update action that I need help.</p>
  
  <p>Here is my sample model code:</p>
  
  <pre><code>class DatabaseContext : DbContext
  {
      protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
      {
          optionsBuilder.UseSqlServer("...connection string...");
      }
  
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Comment&gt;()
              .HasOne(c =&gt; c.Post)
              .WithMany(p =&gt; p.Comments)
              .HasForeignKey(c =&gt; c.PostName)
              .OnDelete(DeleteBehavior.Cascade); 
              //if you delete the post, delete its comments
  
          modelBuilder.Entity&lt;Comment&gt;().HasKey(c =&gt; new { c.Number, c.PostName });
      }
  }
  
  class Post
  {
      [Required, Key, StringLength(100)]
      public string Name { get; set; }
      public string Contents { get; set; }
      public List&lt;Comment&gt; Comments { get; set; }
  }
  
  class Comment
  {
      public int Number { get; set; }
      [Required]
      public Post Post { get; set; }
      [Required, StringLength(100)]
      public string PostName { get; set; }
      public string Contents { get; set; }
  }
  </code></pre>
  
  <p>When I run <code>Add-Migration First</code>, this is the generated code for the <code>Up</code> method.</p>
  
  <pre><code>migrationBuilder.CreateTable(
      name: "Post",
      columns: table =&gt; new
      {
          Name = table.Column&lt;string&gt;(nullable: false),
          Contents = table.Column&lt;string&gt;(nullable: true)
      },
      constraints: table =&gt;
      {
          table.PrimaryKey("PK_Post", x =&gt; x.Name);
      });
  migrationBuilder.CreateTable(
      name: "Comment",
      columns: table =&gt; new
      {
          Number = table.Column&lt;int&gt;(nullable: false),
          PostName = table.Column&lt;string&gt;(nullable: false),
          Contents = table.Column&lt;string&gt;(nullable: true)
      },
      constraints: table =&gt;
      {
          table.PrimaryKey("PK_Comment", x =&gt; new { x.Number, x.PostName });
          table.ForeignKey(
              name: "FK_Comment_Post_PostName",
              column: x =&gt; x.PostName,
              principalTable: "Post",
              principalColumn: "Name",
              onDelete: ReferentialAction.Cascade
              // ,onUpdate: ReferentialAction.Cascade
              );
      });
  </code></pre>
  
  <p>Note that the last parameter is commented out; I added this manually afterwards. If I uncomment the line, the created sql table <strong>does</strong> have <code>ON UPDATE CASCADE</code> applied.</p>
  
  <p>Right now I need to manually alter my migration files to add update cascade support. Is there a way within the Fluent API that I can have the migration engine add <code>onUpdate: ReferentialAction.Cascade</code> for me when the migration file is created?</p>
  
  <p><a href="https://github.com/aspnet/EntityFramework/search?utf8=%E2%9C%93&amp;q=OnUpdate&amp;type=Code" rel="nofollow">Searching through the github code</a> gives some interesting results, and is also how I figured out that manually editing the migration file would work.</p>
  
  <p>In the <code>CSharpMigrationOperationGenerator</code> class, the <a href="https://github.com/aspnet/EntityFramework/blob/cd2872cfe618bb9445df2a6342eb914ada479c3f/src/Microsoft.EntityFrameworkCore.Tools.Core/Migrations/Design/CSharpMigrationOperationGenerator.cs#L132" rel="nofollow"><code>Generate</code></a> method that deals with <code>AddForeignKeyOperation</code> <a href="https://github.com/aspnet/EntityFramework/blob/cd2872cfe618bb9445df2a6342eb914ada479c3f/src/Microsoft.EntityFrameworkCore.Tools.Core/Migrations/Design/CSharpMigrationOperationGenerator.cs#L183" rel="nofollow">has a section</a> for writing <code>onUpdate:</code>, so the generator must have the ability to write the value I need. How do I trigger it?</p>
  
  <hr>
  
  <p>In attempt to an answer given, I added the following after the creation of the foreign key in <code>OnModelCreating</code>:</p>
  
  <pre><code>modelBuilder.Entity&lt;Comment&gt;()
      .Property(p =&gt; p.PostName)
      .ValueGeneratedOnAddOrUpdate();
  </code></pre>
  
  <p>The migration created was identical to the migration created without this code, and thus no change in the resulting sql table.</p>