更改Identity SQL存储的错误(官方教程)

时间:2015-05-17 11:23:20

标签: mysql asp.net-mvc entity-framework

按照官方教程(http://www.asp.net/identity/overview/getting-started/aspnet-identity-using-mysql-storage-with-an-entityframework-mysql-provider),我创建了一个新的MVC项目和我需要的所有文件(MySqlHistoryContext等)。这里没有问题,但教程按'运行'一切都是正确的,我按下'运行'并在屏幕截图中获取错误。

模板定义了" ApplicationUserManager" class,所以StackOverflow中解决的线程对我没用。

Screenshoot

的Web.config

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
  <connectionStrings>
    <!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-Test2-20150517124010.mdf;Initial Catalog=aspnet-Test2-20150517124010;Integrated Security=True" providerName="System.Data.SqlClient" />-->
    <add name="DefaultConnection"
      providerName="MySql.Data.MySqlClient"
      connectionString="Server=localhost;Database=users;Uid=root;Pwd=Password1;"/>
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5.1" />
    <httpRuntime targetFramework="4.5.1" />
  </system.web>
  <system.webServer>
    <modules>
      <remove name="FormsAuthentication" />
    </modules>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <providers>
      <provider invariantName="MySql.Data.MySqlClient"
        type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity"/>
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient"></remove>
      <add name="MySQL Data Provider"
        invariant="MySql.Data.MySqlClient"
        description=".Net Framework Data Provider for MySQL"
        type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.7.2.0"/>
    </DbProviderFactories>
  </system.data>
</configuration>

3 个答案:

答案 0 :(得分:1)

乔,我认为这个教程已经过时了,这就是问题所在。

该教程有评论指向最近做过此事的人。这可能有所帮助。 http://k16c.eu/2014/10/12/asp-net-identity-2-0-mariadb/

附注:最新版本的Connector / NET支持EF迁移。当教程创建时 - 正如它所说 - MySQL不支持迁移。由于Connector / NET支持EF迁移,因此最新版本的MYSQL.Data.Entities也支持它。因此,您可能不必按照这些教程中的描述将更改包含在HistoryContext中。

答案 1 :(得分:1)

乔,

查看代码后,连接字符串对我来说很有趣。你确定这是正确的字符串。 LocaldB是一个SQL的东西。这是VS的默认值,绝对不是MySql db。

我已经使用带有Identity的MySql db查看了很多教程。他们都没有使用这样的字符串。

MySql dev网站列出了正确的连接字符串。

http://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html

答案 2 :(得分:0)

您的ApplicationDbContext课程缺少静态Create方法。

例如:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

此外,您的ApplicationUser课程缺少GenerateUserIdentityAsync方法。例如:

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(
    UserManager<ApplicationUser> manager) {

    var userIdentity = 
        await manager.CreateIdentityAsync(this, 
            DefaultAuthenticationTypes.ApplicationCookie);
    // Add custom user claims here
    return userIdentity;
}