EF Code First - Multiple column index with navigation property
<p>I want to put an index on multiple columns (like stated in <a href="https://stackoverflow.com/q/18889218/637425">this question</a>), but one of the properties is a navigation property without a foreign key property in the model.</p>

<p><strong>TL;DR</strong> at the bottom.</p>

<hr>

<p>My model:</p>

<pre><code>public class User
{
    public int Id { get; set; }
    public string Email { get; set; }
    public virtual Shop Shop { get; set; }
}

public class Shop
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Context : DbContext
{
    public DbSet&lt;User&gt; Users { get; set; }
    public DbSet&lt;Shop&gt; Shops { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity&lt;User&gt;().HasRequired(u =&gt; u.Shop).WithMany();
        modelBuilder.Entity&lt;User&gt;().Property(u =&gt; u.Email).HasMaxLength(256);
    }
}
</code></pre>

<hr>

<p>I use an extension (based on the for mentioned question):</p>

<pre><code>internal static class ModelBuilderExtensions
{
    public static StringPropertyConfiguration AddMultiColumnIndex(this StringPropertyConfiguration config, string indexName, int columnOrder, bool unique = false, bool clustered = false)
    {
        var indexAttribute = new IndexAttribute(indexName, columnOrder) { IsUnique = unique, IsClustered = clustered };
        var indexAnnotation = new IndexAnnotation(indexAttribute);

        return config.HasColumnAnnotation(IndexAnnotation.AnnotationName, indexAnnotation);
    }

    public static PrimitivePropertyConfiguration AddMultiColumnIndex(this PrimitivePropertyConfiguration property, string indexName, int columnOrder, bool unique = false, bool clustered = false)
    {
        var indexAttribute = new IndexAttribute(indexName, columnOrder) { IsUnique = unique, IsClustered = clustered };
        var indexAnnotation = new IndexAnnotation(indexAttribute);

        return property.HasColumnAnnotation(IndexAnnotation.AnnotationName, indexAnnotation);
    }
}
</code></pre>

<p>I wish to create an index as follows:</p>

<pre><code>modelBuilder.Entity&lt;User&gt;().Property(x =&gt; x.Email).AddMultiColumnIndex("UX_User_EmailShopId", 0, unique: true);
modelBuilder.Entity&lt;User&gt;().Property(x =&gt; x.Shop.Id).AddMultiColumnIndex("UX_User_EmailShopId", 1, unique: true);
</code></pre>

<p>But this gives me an error when scaffolding a migration:</p>

<blockquote>
  <p>System.InvalidOperationException: The type 'Shop' has already been
  configured as an entity type. It cannot be reconfigured as a complex
  type.</p>
</blockquote>

<p>When I try to add the index as follows it gives another error:</p>

<pre><code>modelBuilder.Entity&lt;User&gt;().Property(x =&gt; x.Shop).AddMultiColumnIndex("UX_User_EmailShopId", 1, unique: true);
</code></pre>

<blockquote>
  <p>The type 'My.NameSpace.Shop' must be a non-nullable value type in
  order to use it as parameter 'T' in the generic type or method
  'System.Data.Entity.ModelConfiguration.Configuration.StructuralTypeConfiguration.Property(System.Linq.Expressions.Expression>)</p>
</blockquote>

<hr>

<h2>TL;DR</h2>

<p>So my question is, how do I add an index using the fluent-api on the email and shop (id) combination when the shop_id is not defined in my model (and I don't want to define it)?</p>

<p>The resultant SQL should look something like:</p>

<pre><code>CREATE UNIQUE NONCLUSTERED INDEX [UX_User_EmailShopId] ON [dbo].[User]
(
    [Email] ASC,
    [Shop_Id] ASC
)
</code></pre>