2016-03-24 11 views
0

Bu mysql'e bağlanan ve bu hatayı alan bir mvc uygulaması oluşturmak için bu tutorial'u takip ediyorum.MySQL'i Entity Framework'ten bağlarken hata oluştu

türü 'MySql.Data.MySqlClient.MySqlException' bir istisna MySql.Data.dll oluştu ancak kullanıcı kodu işlenmedi, Ek bilgiler: Belirtilen anahtar çok uzundu; max anahtar uzunluğu 1000 bayttır.

Mysql veritabanımdan ayrı olarak öğreticiden gelen her şeyi takip etmekteyim. Yeni veritabanı ve VS2013 .Net 4.5.1'deki yeni MVC Uygulaması. EF 6.1.3 ve MySQL.Data.Entity sürümü 6.9.8'dir.

Hata bu satırda.

context.Database.Create();

Bazı kişilerin aynı soruna sahip olduklarını buldum, ancak çözümleri bu eğiticide olduğu gibi SetInitializer'ı kullanmıyor. Umarım birileri yanlış yaptığımı gösterir.

bağlam sınıfına DbConfigurationTypeAttribute Ekleme: yapılandırma dosyasındaki DbConfiguration türünü ayarlayın uygulama başlangıçta de DbConfiguration.SetConfiguration(new MySqlEFConfiguration()) çağrılması

[DbConfigurationType(typeof(MySqlEFConfiguration))] 

public class MySqlInitializer : IDatabaseInitializer<ApplicationDbContext> 
    { 

     public void InitializeDatabase(ApplicationDbContext context) 
     { 
      if (!context.Database.Exists()) 
      { 
       // if database did not exist before - create it 
       context.Database.Create(); 
      } 
      else 
      { 
       // query to check if MigrationHistory table is present in the database 
       var migrationHistoryTableExists = ((IObjectContextAdapter)context).ObjectContext.ExecuteStoreQuery<int>(
       string.Format(
        "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '{0}' AND table_name = '__MigrationHistory'", "users")); 

       // if MigrationHistory table is not there (which is the case first time we run) - create it 
       if (migrationHistoryTableExists.FirstOrDefault() == 0) 
       { 
        context.Database.Delete(); 
        context.Database.Create(); 
       } 
      } 
     } 
    } 

public class MySqlHistoryContext : HistoryContext 
{ 
    public MySqlHistoryContext(
       DbConnection existingConnection, 
       string defaultSchema) 
     : base(existingConnection, defaultSchema) 
    { 
    } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     base.OnModelCreating(modelBuilder); 
     modelBuilder.Entity<HistoryRow>().Property(h => h.MigrationId).HasMaxLength(100).IsRequired(); 
     modelBuilder.Entity<HistoryRow>().Property(h => h.ContextKey).HasMaxLength(200).IsRequired(); 
    } 
} 

public class MySqlConfiguration : DbConfiguration 
{ 
    public MySqlConfiguration() 
    { 
     SetHistoryContext(
     "MySql.Data.MySqlClient", (conn, schema) => new  MySqlHistoryContext(conn, schema)); 
    } 
} 

public class ApplicationUser : IdentityUser 
    { 
     public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) 
     { 
      // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType 
      var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 
      // Add custom user claims here 
      return userIdentity; 
     } 
    } 

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
    { 
     static ApplicationDbContext() 
     { 
      Database.SetInitializer(new MySqlInitializer()); 
     } 

    public ApplicationDbContext() 
     : base("DefaultConnection", throwIfV1Schema: false) 
    { 
    } 

    public static ApplicationDbContext Create() 
    { 
     return new ApplicationDbContext(); 
    } 
} 

cevap

0

Bu üç şekilde yapılabilir:

<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6"> 
+0

için teşekkürler cevabınızı. bağlam sınıfında, IdentityModels.cs uygulamasında ApplicationDbContext sınıfını mı kastediyorsunuz? Bunu denedim ve yine aynı hatayı aldım ama bu sefer 1000 byte yerine 767 bayt. – lawphotog

+0

Global .asax'te ikinci seçeneği çağırmayı denedim ve SetInitializer yöntemini çağırırken bana bu hatayı verdim. 'MySqlEFConfiguration' örneği oluşturuldu, ancak bu tür 'ApplicationDbContext' bağlamında aynı montajda bulunmadı. – lawphotog

+0

üçüncü seçenek bana ilk seçenekle aynı hatayı veriyor. Herhangi bir öneri Eminem? – lawphotog