我想使用ASP.NET Web API与MySQL一起使用。我按照以下步骤进行了操作:
所以我做了以下事情:
mysql.data
的引用添加到项目中mysql.data.entity
配置:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient"
type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="MySql.Data.MySqlClient"
type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.10.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
</provider>
</providers>
</entityFramework>
我添加了MySqlInitializer
类:
public class MySqlInitializer : IDatabaseInitializer<ApplicationDbContext>
{
public void InitializeDatabase(ApplicationDbContext 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>(
"SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'IdentityMySQLDatabase' AND table_name = '__MigrationHistory'");
// 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();
}
}
}
}
我将ApplicationDbContext
更改为:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
static ApplicationDbContext()
{
Database.SetInitializer(new MySqlInitializer());
}
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
我的连接字符串是:
<connectionStrings>
<add name="DefaultConnection"
connectionString="Database=IdentityMySQLDatabase;Data Source=localhost;User Id=root;Password=Admin!@#"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
我正在使用高级Rest Client测试api。但是,当我想注册一个新用户时,会得到一个空引用。这是我的堆栈跟踪:
MySql.Data.MySqlClient.MySqlProviderServices.GetDbProviderManifestToken(DbConnection连接)
在System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection连接)
在System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices,DbConnection连接)
在System.Data.Entity.Infrastructure.DefaultManifestTokenResolver处。<> c__DisplayClass1.b__0(元组3 k)
2.GetOrAdd(TKey键,Func
at System.Collections.Concurrent.ConcurrentDictionary2 valueFactory)
2.GetValue(TInput输入)
at System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.ResolveManifestToken(DbConnection connection)
at System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy
在System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
在System.Data.Entity.Internal.InternalContext.Initialize()
在System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(TypeEntityType)
在System.Data.Entity.Internal.Linq.InternalSet1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet
1.get_InternalContext() 在 System.Data.Entity.Infrastructure.DbQuery1.System.Linq.IQueryable.get_Provider() at System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync[TSource](IQueryable
1 来源,表达式1 predicate, CancellationToken cancellationToken) at System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync[TSource](IQueryable
1 源,表达式1 predicate) at Microsoft.AspNet.Identity.EntityFramework.UserStore
6.d__67.MoveNext() ---从上一个引发异常的位置开始的堆栈跟踪- System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) Microsoft.AspNet.Identity.TaskExtensions.CultureAwaiter1.GetResult() at Microsoft.AspNet.Identity.UserValidator
2.d__14.MoveNext() ---从上一个引发异常的位置开始的堆栈跟踪- System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) Microsoft.AspNet.Identity.UserValidator2.<ValidateAsync>d__13.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNet.Identity.UserManager
2.d__73.MoveNext() ---从上一个引发异常的位置开始的堆栈跟踪- System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) Microsoft.AspNet.Identity.UserManager2.<CreateAsync>d__79.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter
1.GetResult()位于 中的TestMYSQL.Controllers.AccountController.d__20.MoveNext() D:\ Visual Studio 项目\私人\ TestMYSQL \ TestMYSQL \ Controllers \ AccountController.cs:line 333 ---从先前发生异常的位置开始的堆栈结束跟踪 扔- System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) System.Threading.Tasks.TaskHelpersExtensions.d__1`1.MoveNext() ---从上一个引发异常的位置开始的堆栈跟踪- System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) System.Web.Http.Controllers.ApiControllerActionInvoker.d__1.MoveNext() ---从上一个引发异常的位置开始的堆栈跟踪- System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) System.Web.Http.Controllers.ActionFilterResult.d__5.MoveNext() ---从上一个引发异常的位置开始的堆栈跟踪- System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) System.Web.Http.Filters.AuthorizationFilterAttribute.d__3.MoveNext() ---从上一个引发异常的位置开始的堆栈跟踪- System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) System.Web.Http.Controllers.AuthenticationFilterResult.d__5.MoveNext() ---从上一个引发异常的位置开始的堆栈跟踪- System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()
我想念什么吗?任何帮助,将不胜感激。我已经搜索了2天,但仍然无法解决问题。
谢谢。
答案 0 :(得分:0)
研究了可能性之后。解决方案比我想的要简单。如果有人遇到了同样的问题,这就是我所做的: -添加新项目mvc,其具有用于单个用户登录的标识。 -安装nuget包Mysql.Data -安装nuget包Mysql.Data.EntityFramework -创建自己的mysql数据库并获取连接字符串。 -更改连接字符串并添加提供程序名称'MySql.Data.MySqlClient'
启动并注册用户名,应用程序将在mysql中为您创建一个数据库。
我希望这对遇到相同问题的人有帮助。