指定键太长;最大密钥长度为767字节实体框架中的Mysql错误6 [英] Specified key was too long; max key length is 767 bytes Mysql error in Entity Framework 6

查看:190
本文介绍了指定键太长;最大密钥长度为767字节实体框架中的Mysql错误6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始使用Visual Studio 2012在Asp.net Mvc-5应用程序。所以我已经从nuget下载了Entity Framework-6和MySQL 6.8.3.0。当我尝试通过使用db Context命令创建数据库

  dbContext.Database.CreateIfNotExists(); 

抛出此异常。


指定的键太长;最大密钥长度为767字节


我已经搜索了,但找不到任何解决方案。在我的搜索中有一件事,这可能是Unicode字符的问题。我不知道如何处理这个问题。



已更新



我正在使用以下配置

 < configuration> 
< configSections>
< section name =entityFrameworktype =System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,EntityFramework,Version = 6.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089requirePermission =false/> ;
< / configSections>
< entityFramework>
< providers>
< provider invariantName =MySql.Data.MySqlClienttype =MySql.Data.MySqlClient.MySqlProviderServices,MySql.Data.Entity.EF6/>
< / providers>
< / entityFramework>
< system.data>
< DbProviderFactories>
< remove invariant =MySql.Data.MySqlClient/>
< add name =MySQL数据提供者invariant =MySql.Data.MySqlClientdescription =。用于MySQL的Net Framework数据提供者type =MySql.Data.MySqlClient.MySqlClientFactory,MySql.Data,Version = 6.8.3.0,Culture = neutral,PublicKeyToken = c5687fc88969c44d/>
< / DbProviderFactories>
< /system.data>
< / configuration>

我的DB上下文类。我已经删除了所有的模型,只剩下一个模型

  public class MyContext:DbContext 
{
public MyContext()
:base(myconn)
{
this.Configuration.ValidateOnSaveEnabled = false;
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove< System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>() ;
base.OnModelCreating(modelBuilder);
}

public DbSet< ModelOne> ModelOne {get;组;

}

模特类

  public class ModelOne 
{
[Key]
public int CreatedId {get;组; }
public Nullable< int> UserId {get;组; }
public Nullable< DateTime>日期{get;组; }
public string描述{get;组;
}

任何人都可以帮我解决这个问题?



谢谢。

解决方案

我已经改变了DbContext的DbConfigurationType。
$ b

从此链接 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< System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>() ;
base.OnModelCreating(modelBuilder);
}

public DbSet< ModelOne> ModelOne {get;组; }

}


I have start working on Asp.net Mvc-5 application using visual studio 2012. So I have downloaded Entity Framework-6 and MySQL 6.8.3.0 from nuget. When I tried to create database by using db Context command

dbContext.Database.CreateIfNotExists();

This exception thrown.

Specified key was too long; max key length is 767 bytes

I have done search on it, but cannot find any solution. One thing that I got during my search, this can be Unicode characters problem. I don't know how to deal with this issue.

Updated

I am using following configuration

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient" />
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data>
</configuration>

My DB Context class. I have removed all the models just keep left one model

public class MyContext : DbContext
{
    public MyContext()
        : base("myconn")
    {
        this.Configuration.ValidateOnSaveEnabled = false;
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>();
        base.OnModelCreating(modelBuilder);
    }

    public DbSet<ModelOne> ModelOne { get; set; }

}

Model class

public class  ModelOne
{
        [Key]
        public int CreatedId { get; set; }
        public Nullable<int> UserId { get; set; }
        public Nullable<DateTime> Date { get; set; }
        public string Description { get; set; }
  }

Can anyone help me with this issue?

Thank you.

解决方案

I have changed the DbConfigurationType of DbContext.

Got from this this link stackoverflow

Now it is working

[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<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>();
        base.OnModelCreating(modelBuilder);
    }

    public DbSet<ModelOne> ModelOne { get; set; }

}

这篇关于指定键太长;最大密钥长度为767字节实体框架中的Mysql错误6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆