title: Entity Framework Core 3.1 code first unique constraint not working
body: <p>Well question is pretty clear. Here is my <code>OnModelCreating</code> method in the <code>dbContext</code> class:</p>
  
  <pre><code>modelBuilder.Entity&lt;States&gt;(entity =&gt;
              {
                  entity.ToTable("States");
  
                  modelBuilder.Entity&lt;States&gt;()
                  .HasIndex(p =&gt; new { p.State })
                  .HasFilter("[State] IS NOT NULL")
                  .HasFilter("[Code] IS NOT NULL")
                  .IncludeProperties(p =&gt; new
                  {
                      p.Code
                  })
                  .IsUnique();
  </code></pre>
  
  <p>I have tried <code>.IsUnique(true)</code> too but it doesn't work.</p>
  
  <p>And here is migration code being generated:</p>
  
  <pre><code>protected override void Up(MigrationBuilder migrationBuilder)
  {
          migrationBuilder.CreateTable(
              name: "States",
              columns: table =&gt; new
              {
                  Id = table.Column&lt;int&gt;(nullable: false)
                      .Annotation("SqlServer:Identity", "1, 1"),
                  State = table.Column&lt;string&gt;(maxLength: 30, nullable: true),
                  Code = table.Column&lt;string&gt;(maxLength: 3, nullable: true),
                  Description = table.Column&lt;string&gt;(nullable: true)
              },
              constraints: table =&gt;
              {
                  table.PrimaryKey("PK_States", x =&gt; x.Id);
              });
  
          migrationBuilder.CreateIndex(
              name: "IX_States_State",
              table: "States",
              column: "State",
              unique: true,
              filter: "[Code] IS NOT NULL")
              .Annotation("SqlServer:Include", new[] { "Code" });
  }
  </code></pre>
  
  <p>and here is my SQL query of the table being generated:</p>
  
  <pre><code>CREATE TABLE [dbo].[States]
  (
      [Id] [INT] IDENTITY(1,1) NOT NULL,
      [State] [NVARCHAR](30) NULL,
      [Code] [NVARCHAR](3) NULL,
      [Description] [NVARCHAR](MAX) NULL,
  
      CONSTRAINT [PK_States] 
          PRIMARY KEY CLUSTERED ([Id] ASC)
                      WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
                            IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, 
                            ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  GO
  </code></pre>
  
  <p>As you can see the columns are not unique and are nullable.</p>
  
  <p>What am I missing here?</p>