Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IndexAttribute or IndexAnnotation not working while initializing MySQL #51

Closed
euyuil opened this issue Aug 22, 2016 · 3 comments
Closed

Comments

@euyuil
Copy link

euyuil commented Aug 22, 2016

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.

Screenshots from MySQL Workbench:

s1

s2

Full code and project files:

eftest.zip

@divega
Copy link
Contributor

divega commented Aug 23, 2016

@euyuil I am not familiar enough with the MySQL provider to know for sure, but this might be an expected limitation if support for index migrations operations was not implemented. If you can prove that this works with SQL Server but not with MySQL, I would encourage you to file a bug. I believe https://bugs.mysql.com/ is the right place to do so.

cc @gmartinezsan.

@divega
Copy link
Contributor

divega commented Aug 31, 2016

EF Team Triage: Closing as with the current information we believe this issue is external to EF and not actionable for the EF team. Hopefully the information provided can help redirect the issue to the owners of the MySQL provider where it can be addressed.

@gmartinezsan
Copy link

Hi, I added a bug so you can follow up @euyuil if you like. We'll check if there is any issue in the MySQL provider.

Here's the link of the bug.
http://bugs.mysql.com/bug.php?id=82828

Feel free to subscribe.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants