空间/全文/散列索引和显式索引顺序 EF 的错误使用

Posted

技术标签:

【中文标题】空间/全文/散列索引和显式索引顺序 EF 的错误使用【英文标题】:Incorrect usage of spatial/fulltext/hash index and explicit index order EF 【发布时间】:2018-12-03 06:27:22 【问题描述】:

我在尝试登录时收到Incorrect usage of spatial/fulltext/hash index and explicit index order 这个错误。

我没有使用实体框架迁移。

[DbConfigurationType(typeof(mysqlEFConfiguration))]
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

    public ApplicationDbContext()
        : base("DefaultConnection")
    
    

    static ApplicationDbContext()
    
        // Set the database intializer which is run once during application start
        // This seeds the database with admin user credentials and admin role
        Database.SetInitializer<ApplicationDbContext>(new CreateDatabaseIfNotExists<ApplicationDbContext>());
    

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


    public virtual DbSet<Category> Categories  get; set; 

这是我在 MySQL 中自动生成数据库的代码。我没有迁移文件。

【问题讨论】:

您是否明确定义了索引的升序或降序?如果是,请将其删除,然后尝试 不,我没有定义升序或降序。它是自动生成的。 【参考方案1】:

我遇到了同样的问题,我用继承自 MySqlMigrationSqlGenerator 的自定义类修复了它,然后我重写了 protected override MigrationStatement Generate (CreateIndexOperation op) 方法,然后我创建一个继承自 DbConfiguration 的新 EFConfiguration 类,并使用新类设置 SetMigrationSqlGenerator。这是代码:

using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Migrations.History;
using System.Data.Entity.Migrations.Model;
using System.Data.Entity.Migrations.Sql;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.EntityFramework;
using MySql.Data.MySqlClient;

namespace customSqlGenerator.Contexto

    /// <summary>
    /// Inherit from MySqlMigrationSqlGenerator by fixing the Index generation method, set the default type in BTREE 
    /// <para>(the MySql manual says that in the SQL script syntax it is not necessary to specify it for the innoDB engine)</para>
    /// </summary>
    public class MyCustomMigrationSQLGenerator : MySqlMigrationSqlGenerator
    
        private string TrimSchemaPrefix ( string table )
        
            if ( table.StartsWith ( "dbo." ) )
                return table.Replace ( "dbo.", "" );
            return table;
        

        protected override MigrationStatement Generate ( CreateIndexOperation op )
        
            StringBuilder sb = new StringBuilder ( );

            sb = sb.Append ( "CREATE " );

            if ( op.IsUnique )
            
                sb.Append ( "UNIQUE " );
            

            sb.AppendFormat ( "index  `0` on `1` (", op.Name, TrimSchemaPrefix ( op.Table ) );
            sb.Append ( string.Join ( ",", op.Columns.Select ( c => "`" + c + "`" ) ) + ") " );

            return new MigrationStatement ( )  Sql = sb.ToString ( ) ;
        
    

    /// <summary>
    /// Inherit from DbConfiguration to set the new QSL script generator and set the conditions to work with MySQL
    /// </summary>
    public class MyCustomEFConfiguration : DbConfiguration
    
        public MyCustomEFConfiguration ()
        
            AddDependencyResolver ( new MySqlDependencyResolver ( ) );
            SetProviderFactory ( MySqlProviderInvariantName.ProviderName, new MySqlClientFactory ( ) );
            SetProviderServices ( MySqlProviderInvariantName.ProviderName, new MySqlProviderServices ( ) );
            SetDefaultConnectionFactory ( new MySqlConnectionFactory ( ) );
            SetMigrationSqlGenerator ( MySqlProviderInvariantName.ProviderName, () => new MyCustomMigrationSQLGenerator ( ) );
            SetManifestTokenResolver ( new MySqlManifestTokenResolver ( ) );
            SetHistoryContext ( MySqlProviderInvariantName.ProviderName, 
                ( existingConnection, defaultSchema ) => new MyCustomHistoryContext ( existingConnection, defaultSchema ) );
        
    

    /// <summary>
    /// Read and write the migration history of the database during the first code migrations. This class must be in the same assembly as the EF configuration
    /// </summary>
    public class MyCustomHistoryContext : HistoryContext
    
        public MyCustomHistoryContext ( DbConnection existingConnection, string defaultSchema ) : base ( existingConnection, defaultSchema )  

        protected override void OnModelCreating ( System.Data.Entity.DbModelBuilder modelBuilder )
        
            base.OnModelCreating ( modelBuilder );
            modelBuilder.Entity<HistoryRow> ( ).HasKey ( h => new  h.MigrationId  );
        
    


然后,在我的 DBContext 上,我添加了带有配置的属性,例如:

[DbConfigurationType ( typeof ( MyCustomEFConfiguration ) )]
public class DataBaseContexto : IdentityDbContext<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
   
    public DataBaseContexto () : base ( "defaultConnection" )
               
        this.Configuration.LazyLoadingEnabled = true;
    

【讨论】:

以上是关于空间/全文/散列索引和显式索引顺序 EF 的错误使用的主要内容,如果未能解决你的问题,请参考以下文章

正确理解Mysql的列索引和多列索引

EF6 中的 Eager 、 Lazy 和显式加载

[转帖]B树索引位图索引和散列索引

Windows提供了两种将DLL映像到进程地址空间的方法(隐式和显式)

全文搜索的索引列

隐式转换和显式转换及强制转换的区别