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

时间:2014-12-29 13:55:38

标签: c# mysql entity-framework visual-studio-2013 asp.net-identity-2

我想将ASP.NET Identity 2与使用MySQL的Entity Framework一起用于存储,但我无法让它工作。

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

  

为属性指定了冲突的配置设置   类型上的'UserName'   “Microsoft.AspNet.Identity.EntityFramework.IdentityUser`4 [[System.String,   mscorlib,版本= 4.0.0.0,文化=中性,   公钥= b77a5c561934e089],[Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin,   Microsoft.AspNet.Identity.EntityFramework,Version = 2.0.0.0,   文化=中性,   公钥= 31bf3856ad364e35],[Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole,   Microsoft.AspNet.Identity.EntityFramework,Version = 2.0.0.0,   文化=中性,   公钥= 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" />

项目中唯一的代码是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
        }
    }
}

using Microsoft.AspNet.Identity.EntityFramework;

namespace Models
{
    public class ApplicationUser : IdentityUser
    {
        // add whatever properties you want your user to have here
    }
}

要使项目编译,我还必须添加Nuget包“Microsoft ASP.NET Identity EntityFramework”。

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

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

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

如果还有其他方法可以将Identity 2.0与Entity Framework和MySQL一起使用,请告诉我们。我不必像这样做,我只是认为这是一个好方法。

4 个答案:

答案 0 :(得分:8)

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

来自

    modelBuilder
        .Entity<IdentityUser>()
        .Property(p => p.UserName)
        .HasMaxLength(255);

    modelBuilder
        .Entity<ApplicationUser>()
        .Property(p => p.UserName)
        .HasMaxLength(255);

答案 1 :(得分:1)

我已在您在问题中引用的博客文章中回答了您的问题。 stianskj与他的解决方法完全正确,它可能是目前回滚到EF 6.1.1的最简单方法。

已经在EF错误跟踪器中发布了一个问题:http://entityframework.codeplex.com/workitem/2630

该错误似乎与8月份进行的重构有关,另一个错误与之相关。所以EF6 vNext可能会包含一个修复程序。

答案 2 :(得分:0)

尝试将ApplicationUser上UserName的长度限制为与IdentityUser相同。 我有与EF 6.1.2类似的问题,尝试回滚到6.1.1

答案 3 :(得分:0)

尝试覆盖自定义ApplicationUser中的UserName属性并指定MaxLengthAttributeStringLengthAttribute