C# 未创建MySQL实体框架表

C# 未创建MySQL实体框架表,c#,mysql,entity-framework,signalr,C#,Mysql,Entity Framework,Signalr,遵循本指南: 我已尝试使用实体框架建立与MySQL数据库的连接。 连接成功通过,但每当我运行代码时,就会出现以下错误: MySql.Data.MySqlClient.MySqlException:表“helpcontext.users”不存在 我的上下文类如下所示: [DbConfigurationType(typeof(MySqlEFConfiguration))] public class HelpContext : DbContext { public DbSet<User&

遵循本指南: 我已尝试使用实体框架建立与MySQL数据库的连接。
连接成功通过,但每当我运行代码时,就会出现以下错误:

MySql.Data.MySqlClient.MySqlException:表“helpcontext.users”不存在

我的上下文类如下所示:

[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class HelpContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Channel> Channels { get; set; }
    public DbSet<ChatMessage> ChatMessages { get; set; }
    public DbSet<Question> Questions { get; set; }
    public DbSet<QuestionComment> QuestionComments { get; set; }

    public HelpContext() : base()
    {
        Database.SetInitializer(new MySqlInitializer());
    }
}
public class MySqlInitializer : IDatabaseInitializer<HelpContext>
{
    public void InitializeDatabase(HelpContext 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'",
              "helpcontext"));

            // 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();
            }
        }
    }
}
[DbConfigurationType(typeof(MySqlEFConfiguration))]
公共类HelpContext:DbContext
{
公共数据库集用户{get;set;}
公共数据库集通道{get;set;}
公共数据库集聊天信息{get;set;}
公共数据库集问题{get;set;}
公共数据库集问题注释{get;set;}
public HelpContext():base()
{
SetInitializer(新的MySqlInitializer());
}
}
我的“MySQLInitializer”类如下所示:

[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class HelpContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Channel> Channels { get; set; }
    public DbSet<ChatMessage> ChatMessages { get; set; }
    public DbSet<Question> Questions { get; set; }
    public DbSet<QuestionComment> QuestionComments { get; set; }

    public HelpContext() : base()
    {
        Database.SetInitializer(new MySqlInitializer());
    }
}
public class MySqlInitializer : IDatabaseInitializer<HelpContext>
{
    public void InitializeDatabase(HelpContext 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'",
              "helpcontext"));

            // 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();
            }
        }
    }
}
公共类MySqlInitializer:IDatabaseInitializer
{
public void InitializeDatabase(HelpContext上下文)
{
如果(!context.Database.Exists())
{
//如果数据库以前不存在-创建它
context.Database.Create();
}
其他的
{
//查询以检查数据库中是否存在MigrationHistory表
var migrationHistoryTableExists=((IOObjectContextAdapter)上下文)。ObjectContext.ExecuteStoreQuery(
字符串格式(
“从信息_schema.tables中选择COUNT(*),其中表_schema='{0}'和表_name=''uuu MigrationHistory'”,
“帮助上下文”);
//如果MigrationHistory表不存在(这是我们第一次运行时的情况)-创建它
if(migrationHistoryTableExists.FirstOrDefault()==0)
{
context.Database.Delete();
context.Database.Create();
}
}
}
}
模式是在mysql实例上创建的,但是没有创建任何表,这导致应用程序因上述错误而崩溃。 知道我做错了什么吗

编辑: 事实证明,这个问题源于之前的一次尝试,在没有创建表的情况下,针对我的原始问题采用了其他解决方案。 我的上下文类中有这段代码

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<User>().MapToStoredProcedures();
        modelBuilder.Entity<Channel>().MapToStoredProcedures();
        modelBuilder.Entity<ChatMessage>().MapToStoredProcedures();
        modelBuilder.Entity<Question>().MapToStoredProcedures();
        modelBuilder.Entity<QuestionComment>().MapToStoredProcedures();
    }
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
基于模型创建(modelBuilder);
modelBuilder.Entity().MapToStoredProcess();
modelBuilder.Entity().MapToStoredProcess();
modelBuilder.Entity().MapToStoredProcess();
modelBuilder.Entity().MapToStoredProcess();
modelBuilder.Entity().MapToStoredProcess();
}

结果证明这很糟糕。从我的类中删除该方法修复了这个问题

原来我的代码中还有一个问题。有关详细信息,请参阅编辑。

helpcontext是架构吗?MySQL忽略模式创建。也许这是你的问题。尝试从实体/映射中删除架构定义。Ehm什么?类似于“string.Format”(“SELECT COUNT(*)FROM information_schema.tables,其中table_schema='{0}'和table_name='._MigrationHistory',“somethingElse”);”相反