具有mysql而不是SQL Server空引用的ASP.NET身份

时间:2018-09-30 12:08:41

标签: asp.net-web-api

我想使用ASP.NET Web API与MySQL一起使用。我按照以下步骤进行了操作:

https://docs.microsoft.com/en-us/aspnet/identity/overview/getting-started/aspnet-identity-using-mysql-storage-with-an-entityframework-mysql-provider

所以我做了以下事情:

  1. 安装mysql连接器并通过工作台在本地创建mysql数据库
  2. 将对mysql.data的引用添加到项目中
  3. 通过nuget管理器安装了mysql.data.entity
  4. 配置确实自动添加了mysql提供程序

配置:

<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>
  1. 我添加了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();
                }
            }
        }
    }
    
  2. 我将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)
at System.Collections.Concurrent.ConcurrentDictionary
2.GetOrAdd(TKey键,Func 2 valueFactory)
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
2.GetValue(TInput输入)
  在System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
  在System.Data.Entity.Internal.InternalContext.Initialize()
  在System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(TypeEntityType)
  在System.Data.Entity.Internal.Linq.InternalSet 1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet 1.get_InternalContext()   在   System.Data.Entity.Infrastructure.DbQuery 1.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.CultureAwaiter 1.GetResult() at Microsoft.AspNet.Identity.UserValidator 2.d__14.MoveNext()   ---从上一个引发异常的位置开始的堆栈跟踪-   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)   Microsoft.AspNet.Identity.UserValidator 2.<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.UserManager 2.<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天,但仍然无法解决问题。

谢谢。

1 个答案:

答案 0 :(得分:0)

研究了可能性之后。解决方案比我想的要简单。如果有人遇到了同样的问题,这就是我所做的: -添加新项目mvc,其具有用于单个用户登录的标识。 -安装nuget包Mysql.Data -安装nuget包Mysql.Data.EntityFramework -创建自己的mysql数据库并获取连接字符串。 -更改连接字符串并添加提供程序名称'MySql.Data.MySqlClient'

启动并注册用户名,应用程序将在mysql中为您创建一个数据库。

我希望这对遇到相同问题的人有帮助。