使用MySQL和实体框架的ASP.NET标识

mad*_*cap 2 c# mysql entity-framework visual-studio-2013 asp.net-identity-2

我想使用ASP.NET Identity 2和Entity Framework一起使用MySQL进行存储,但我无法让它工作.

我试图按照http://k16c.eu/2014/10/12/asp-net-identity-2-0-mariadb/中的步骤操作,但在运行"启用迁移"时,会出现以下错误:

在类型'Microsoft.AspNet.Identity.EntityFramework.IdentityUser`4 [[System.String,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[Microsoft]上为属性'UserName'指定了冲突的配置设置. AspNet.Identity.EntityFramework.IdentityUserLogin,Microsoft.AspNet.Identity.EntityFramework,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35],[Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole,Microsoft.AspNet.Identity.EntityFramework, Version = 2.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35],[Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim,Microsoft.AspNet.Identity.EntityFramework,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35]]' :MaxLength = 256与MaxLength = 128冲突

这就是我所做的:我创建了一个只有类库项目的新解决方案.对于该项目,我已经为Entity Framework和MySQL添加了Nuget包(如上文所述),并通过添加连接字符串修改了app.config:

<add name="MyConnection" connectionString="Datasource=localhost;Database=test1;uid=user;pwd=password;Convert Zero Datetime=True;Allow User Variables=True;" providerName="MySql.Data.MySqlClient" />
Run Code Online (Sandbox Code Playgroud)

项目中唯一的代码是ApplicationContext和ApplicationUser这两个类:

using System.Data.Entity;
using Microsoft.AspNet.Identity.EntityFramework;
using MySql.Data.Entity;

namespace Models
{
    [DbConfigurationType(typeof(MySqlEFConfiguration))]
    public class ApplicationContext : IdentityDbContext<ApplicationUser>
    {
        /// <summary>
        /// To use this constructor you have to have a connection string
        /// name "MyConnection" in your configuration
        /// </summary>
        public ApplicationContext() : this("MyConnection") { }

        /// <summary>
        /// Construct a db context
        /// </summary>
        /// <param name="connStringName">Connection to use for the database</param>
        public ApplicationContext(string connStringName) : base(connStringName) { }

        /// <summary>
        /// Some database fixup / model constraints
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            #region Fix asp.net identity 2.0 tables under MySQL
            // Explanations: primary keys can easily get too long for MySQL's
            // (InnoDB's) stupid 767 bytes limit.
            // With the two following lines we rewrite the generation to keep
            // those columns "short" enough
            modelBuilder.Entity<IdentityRole>()
            .Property(c => c.Name)
            .HasMaxLength(128)
            .IsRequired();

            // We have to declare the table name here, otherwise IdentityUser
            // will be created
            modelBuilder.Entity<IdentityUser>()
            .ToTable("AspNetUsers")
            .Property(c => c.UserName)
            .HasMaxLength(128)
            .IsRequired();
            #endregion
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

using Microsoft.AspNet.Identity.EntityFramework;

namespace Models
{
    public class ApplicationUser : IdentityUser
    {
        // add whatever properties you want your user to have here
    }
}
Run Code Online (Sandbox Code Playgroud)

为了使项目编译,我还必须添加Nuget包"Microsoft ASP.NET Identity EntityFramework".

我做的最后一件事是运行"启用迁移",当我收到有关冲突的maxlength属性的错误时.

我使用Visual Studio 2013更新4和所有Nuget包的最新版本.

任何想法如何解决这个问题?

如果有任何其他方法将Identity 2.0与Entity Framework和MySQL一起使用,请告诉我.我不必像这样做,我只是觉得这是一个好方法.

Fra*_*ier 8

我通过修改我ApplicationDbContextOnModelCreating方法修复了类似的问题:

    modelBuilder
        .Entity<IdentityUser>()
        .Property(p => p.UserName)
        .HasMaxLength(255);
Run Code Online (Sandbox Code Playgroud)

    modelBuilder
        .Entity<ApplicationUser>()
        .Property(p => p.UserName)
        .HasMaxLength(255);
Run Code Online (Sandbox Code Playgroud)