Description:
From the issue reported in github (https://github.com/aspnet/EntityFramework6/issues/51#event-774133112)
I haven't tested with SQL Server yet and am not sure if the issue is only for MySQL.
Package versions:
EntityFramework 6.1.3
MySql.Data 6.9.9
MySql.Data.Entity 6.9.9
Simplified code:
public class MyDbContext : DbContext
{
public MyDbContext() : base("MyDbContextConnectionString")
{
Database.SetInitializer(new MyDbInitializer());
}
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// The following code doesn't work either.
// modelBuilder.Entity<User>()
// .Property(e => e.Mobile)
// .HasColumnAnnotation(
// IndexAnnotation.AnnotationName,
// new IndexAnnotation(new IndexAttribute("IX_Mob") { IsUnique = true }));
}
}
public class MyDbInitializer : DropCreateDatabaseAlways<MyDbContext>
{
}
public class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
[Index(IsUnique = true)]
public string Mobile { get; set; }
}
internal static void Main(string[] args)
{
using (var context = new MyDbContext())
{
// ...
}
}
I expect the table to be created with a primary key Id and a unique index for column Mobile. But the result was that a primary key index for Id was created, and a redundant unique index for Id was also created, but the expected unique index for Mobile was not.
How to repeat:
Please see description.
Suggested fix:
NA