指定密钥太长; 最大密钥长度为767字节Entity Framework 6中的Mysql错误
我已经开始使用visual studio 2012开发Asp.net Mvc-5应用程序了。所以我从nuget下载了Entity Framework-6和MySQL 6.8.3.0。 当我尝试使用db Context命令创建数据库时
dbContext.Database.CreateIfNotExists();
抛出此exception。
指定密钥太长; 最大密钥长度为767字节
我已经对它进行了搜索,但找不到任何解决方案。 我在搜索过程中得到的一件事就是Unicode字符问题。 我不知道如何处理这个问题。
更新
我正在使用以下配置
我的数据库上下文类。 我已经删除了所有模型,只留下一个模型
public class MyContext : DbContext { public MyContext() : base("myconn") { this.Configuration.ValidateOnSaveEnabled = false; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove(); base.OnModelCreating(modelBuilder); } public DbSet ModelOne { get; set; } }
模特课
public class ModelOne { [Key] public int CreatedId { get; set; } public Nullable UserId { get; set; } public Nullable Date { get; set; } public string Description { get; set; } }
任何人都可以帮我解决这个问题吗?
谢谢。
我更改了DbContext的DbConfigurationType。
得到了这个链接stackoverflow
现在它正在运作
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))] public class MyContext : DbContext { public MyContext() : base("myconn") { this.Configuration.ValidateOnSaveEnabled = false; } static MyContext() { DbConfiguration.SetConfiguration(new MySql.Data.Entity.MySqlEFConfiguration()); } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove(); base.OnModelCreating(modelBuilder); } public DbSet ModelOne { get; set; } }
如果您正在使用ASP.NET Identity,那么有3个地方会发生这种情况
- 在迁移历史记录表中
- IdentityRole的Name属性
- ApplicationUser的用户名和电子邮件属性
我遇到的大多数文章都有一个解决方案,基本上将用户名和电子邮件的长度减少到128个字符。 但是,我发现这是不可接受的,因为电子邮件地址的官方规范是256 ansi字符。
我的解决方案是:
- 关闭电子邮件和用户名的unicode
- 覆盖MySqlMigrationSqlGenerator并告诉它使用latin1_general_ci collate,这是ansi字符集。
- 减少角色名称的长度。 这是可以接受的,因为角色名称不需要那么长
- 减少历史迁移的密钥长度,如Internet上的各种文章所述。
您可以在https://github.com/timdinhdotcom/MySql.AspNetIdentity找到我的解决方案
看看这篇文章 ,看看它是否有帮助。 特别是添加了MySqlHistoryContext.cs
, Configuration.cs
和MySqlInitializer.cs
类。
您可以在这里找到解决方案。
public class MySqlHistoryContext : HistoryContext { public MySqlHistoryContext(DbConnection connection, string defaultSchema) : base(connection, defaultSchema) { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity().Property(h => h.MigrationId).HasMaxLength(100).IsRequired(); modelBuilder.Entity ().Property(h => h.ContextKey).HasMaxLength(200).IsRequired(); } }
使用下面的配置代码…这解决了我的问题:
internal sealed class Configuration : DbMigrationsConfiguration { public Configuration() { AutomaticMigrationsEnabled = false; CommandTimeout = 3600; DbConfiguration.SetConfiguration(new MySql.Data.Entity.MySqlEFConfiguration()); SetSqlGenerator(MySql.Data.Entity.MySqlProviderInvariantName.ProviderName, new MySql.Data.Entity.MySqlMigrationSqlGenerator()); SetHistoryContextFactory(MySql.Data.Entity.MySqlProviderInvariantName.ProviderName, (connection, schema) => new MySql.Data.Entity.MySqlHistoryContext(connection, schema)); } }
如果你已经尝试了这篇文章中的所有答案并仍然得到错误,那么尝试在MySQL服务器上运行此命令:
set GLOBAL storage_engine='InnoDb';
这里报告了这个错误: http : //bugs.mysql.com/bug.php?id = 4541
你必须看到这个链接https://stackoverflow.com/a/27082231/929740
- 在上下文类中添加DbConfigurationTypeAttribute:[DbConfigurationType(typeof(MySqlEFConfiguration))]
- 在应用程序启动时调用DbConfiguration.SetConfiguration(new MySqlEFConfiguration())
- 在配置文件中设置DbConfiguration类型:
我通过运行来摆脱这个问题:
Enable-Migrations -EnableAutomaticMigrations -Force -ContextTypeName ApplicationDbContext
并且无需创建初始迁移:
Update-Database
创建的表没有任何问题也没有错误
有一种简单的方法可以在MYSQl中将NVARCHAR字段更改为varchar,在IdentityModels.cs中添加这一行
[DbConfigurationType(typeof(MySqlEFConfiguration))] public class ApplicationDbContext : IdentityDbContext { protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); //troca todos os campos NVARCHAR por varchar modelBuilder.Properties().Where(x => x.PropertyType.FullName.Equals("System.String") && !x.GetCustomAttributes(false).OfType().Where(q => q.TypeName != null && q.TypeName.Equals("varchar(max)", StringComparison.InvariantCultureIgnoreCase)).Any()) .Configure(c => c.HasColumnType("varchar(65000)")); modelBuilder.Properties().Where(x => x.PropertyType.FullName.Equals("System.String") && !x.GetCustomAttributes(false).OfType ().Where(q => q.TypeName != null && q.TypeName.Equals("nvarchar", StringComparison.InvariantCultureIgnoreCase)).Any()) .Configure(c => c.HasColumnType("varchar")); } public ApplicationDbContext() : base("DefaultConnection") { } public static ApplicationDbContext Create() { return new ApplicationDbContext(); } }