遵循本指南:http://www.asp.net/identity/overview/getting-started/aspnet-identity-using-mysql-storage-with-an-entityframework-mysql-provider 我试图使用Entity Framework建立与MySQL数据库的连接 连接成功完成,但每当我运行我的代码时,我都会收到以下错误:
MySql.Data.MySqlClient.MySqlException:Table' helpcontext.users'不存在
我的Context类看起来像这样:
[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());
}
}
My&#34; MySQLInitializer&#34; class看起来像这样:
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();
}
}
}
}
Schema是在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();
}
结果证明这很糟糕。从我的类中删除该方法修复了该问题。
答案 0 :(得分:0)
原来我的代码中还有另一个问题。有关更多信息,请参阅编辑。