我正在尝试与EF6建立一对多关系
这是我的实体。
图书:
public class Book
{
public int BookId { get; set; }
public string BookName { get; set; }
}
发布者:
public class Publisher
{
public int PublisherId { get; set; }
public string PublisherName { get; set; }
public ICollection<Book> Books { get; set; }
}
上下文:
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class MyContext : DbContext
{
public MyContext() : base("name=MySqlDbConnectionString")
{
Database.SetInitializer(new DropCreateDatabaseAlways<MyContext>());
}
public DbSet<Publisher> Publishers { get; set; }
public DbSet<Book> Books { get; set; }
}
程序:
static void Main(string[] args)
{
using (MyContext entities = new MyContext())
{
Book user = new Book() { BookName = "gdsag" };
entities.Books.Add(user);
entities.SaveChanges();
}
}
该问题已用于自动创建数据库。 但是当我运行它时,出现以下错误:
MySql.Data.MySqlClient.MySqlException:'不正确使用空间/全文/哈希索引和显式索引顺序'
我正在使用EF 6.2
MySql.Data 6.9
MySql.Data.Entity 6.9
任何想法我在做什么错误?
预先感谢
修改: 这些是生成sql命令
create table `Books` (`BookId` int not null auto_increment ,`BookName` longtext,`Publisher_PublisherId` int,primary key ( `BookId`) ) engine=InnoDb auto_increment=0
-- Executing at 7/2/2018 11:02:32 PM +03:00
-- Completed in 52 ms with result: 0
create table `Publishers` (`PublisherId` int not null auto_increment ,`PublisherName` longtext,primary key ( `PublisherId`) ) engine=InnoDb auto_increment=0
-- Executing at 7/2/2018 11:02:32 PM +03:00
-- Completed in 49 ms with result: 0
CREATE index `IX_Publisher_PublisherId` on `Books` (`Publisher_PublisherId` DESC) using HASH
-- Executing at 7/2/2018 11:02:32 PM +03:00
-- Failed in 2 ms with error: Incorrect usage of spatial/fulltext/hash index and explicit index order
Disposed transaction at 7/2/2018 11:02:32 PM +03:00
答案 0 :(得分:1)
我最近将.NET的sql客户端库更新为8.X。我正在构建一个框架应用程序(不是CORE),并且我主要使用EF实体的数据注释(不是流利的)配置,但遇到了同样的问题。数据上下文不会初始化,并且会生成错误“空间/全文/哈希索引和显式索引顺序的错误使用”。事实证明,MySQL开发人员似乎忽略了这种sceniaro测试套件。我最终在MySqlMigrationSqlGenerator中重写了CreateIndexOperation生成器,并将默认的“ using HASH”替换为“ using BTREE”。现在我的索引是正确创建的外键关系。
答案 1 :(得分:1)
当然,我要做的是从MySqlMigrationSqlGenerator继承一个类,然后在DbMigrationsConfiguration处理程序中激活了该类。首先列出该类,并提供迁移处理程序的摘录
public class FixedMySqlMigrationSqlGenerator : MySqlMigrationSqlGenerator
{
public FixedMySqlMigrationSqlGenerator()
:base()
{
}
/// <summary>
/// we want BTREE because HASH is not correct for normal Keys on MySQL 8
/// </summary>
/// <param name="op"></param>
/// <returns></returns>
protected override MigrationStatement Generate(CreateIndexOperation op)
{
MigrationStatement migrationStatement = base.Generate(op);
System.Diagnostics.Trace.WriteLine(migrationStatement.Sql);
string fubarSql = migrationStatement.Sql.TrimEnd();
if(fubarSql.EndsWith("using HASH",StringComparison.OrdinalIgnoreCase))
{
string modSql = fubarSql.Replace("using HASH", " using BTREE");
migrationStatement.Sql = modSql;
}
return migrationStatement;
}
}
我也有一个dbmigrationsconfiguration类
public sealed class LogDataMigration : DbMigrationsConfiguration<LogDataContext>
{
public const string CONTEXT_KEY = "BalsamicSoftware.LogData";
private static readonly HashSet<string> _InitializedConnections = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
private static readonly object _LockProxy = new object();
public LogDataMigration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
ContextKey = CONTEXT_KEY;
SetSqlGenerator("MySql.Data.MySqlClient", new FixedMySqlMigrationSqlGenerator());
}
}
通过静态调用
激活迁移配置类Database.SetInitializer<LogDataContext>(new MigrateDatabaseToLatestVersion<LogDataContext, LogDataMigration>(true));
因此,这是三个步骤,首先创建一个迁移配置(如果您还没有的话)。实现sql生成器,然后从您的迁移配置中调用sql生成器。