将Aspnet Identity 2添加到Code First EF模​​型中

时间:2015-05-21 19:53:42

标签: c# asp.net-mvc entity-framework

我正在使用Entity Framework和Code First方法。我创建了一个MVC应用程序并注册了#39;它生成了所有的AspNet表。

我想拥有一个AspNetUsers模型,所以我的其他模型之一可以参考它。 我尝试添加如下所示的映射,但我仍然收到There is already an object named 'AspNetUsers' in the database.错误。

AspNetUser.cs

   public class AspNetUser
    {
        public string Id { get; set; }
        public string Email { get; set; }
        public bool EmailConfirmed { get; set; }
        public string PasswordHash { get; set; }
        public string SecurityStamp { get; set; }
        public string PhoneNumber { get; set; }
        public bool PhoneNumberConfirmed { get; set; }
        public bool TwoFactorEnabled { get; set; }
        public DateTime? LockoutEndDateUtc { get; set; }
        public bool LockoutEnabled { get; set; }
        public int AccessFailedCount { get; set; }
        public string UserName { get; set; }
    }

映射

 public class AspNetUserMap : EntityTypeConfiguration<AspNetUser>
    {
        public AspNetUserMap()
        {
            // Primary Key
            this.HasKey(t => t.Id);

            // Properties
            this.Property(t => t.Id)
                .IsRequired()
                .HasMaxLength(128);

            this.Property(t => t.Email)
                .HasMaxLength(256);

            this.Property(t => t.UserName)
                .IsRequired()
                .HasMaxLength(256);

            // Table & Column Mappings
            this.ToTable("AspNetUsers");
            this.Property(t => t.Id).HasColumnName("Id");
            this.Property(t => t.Email).HasColumnName("Email");
            this.Property(t => t.EmailConfirmed).HasColumnName("EmailConfirmed");
            this.Property(t => t.PasswordHash).HasColumnName("PasswordHash");
            this.Property(t => t.SecurityStamp).HasColumnName("SecurityStamp");
            this.Property(t => t.PhoneNumber).HasColumnName("PhoneNumber");
            this.Property(t => t.PhoneNumberConfirmed).HasColumnName("PhoneNumberConfirmed");
            this.Property(t => t.TwoFactorEnabled).HasColumnName("TwoFactorEnabled");
            this.Property(t => t.LockoutEndDateUtc).HasColumnName("LockoutEndDateUtc");
            this.Property(t => t.LockoutEnabled).HasColumnName("LockoutEnabled");
            this.Property(t => t.AccessFailedCount).HasColumnName("AccessFailedCount");
            this.Property(t => t.UserName).HasColumnName("UserName");
        }
    }

的DbContext

public class MovieContext : DbContext
    {
        public MovieContext() : base("DatabaseName")
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<MovieContext, Migrations.Configuration>("DatabaseName"));
        }

        public DbSet<Movie> Movies { get; set; }
        public DbSet<Genre> Genres { get; set; }
        public DbSet<Log> Logs { get; set; }
        public DbSet<AspNetUser> Users { get; set; }
        public DbSet<Tracking> Tracking { get; set; }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new AspNetUserMap());
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }

更新:

我想我是以错误的方式提问。

基本上我该怎么做:

public class Tracking
{
    public int TrackingId { get; set; }
    public Guid UserId { get; set; } <--This needs to be the ID in the AspNetUsers table
    public ICollection<Movie> MovieId { get; set; }
}

1 个答案:

答案 0 :(得分:-1)

我没有成功使用代码首先为身份实体定制架构。我最终修改了EF生成的迁移代码,并且确保在没有仔细合并的情况下不运行add-migration Initial -force。这也允许我将聚集索引更改为datetime2列,绕过可怕的performance problems of keeping a clustered index on a guid

编辑:如果您尝试更改用户实体的主键类型,则需要使用您的密钥选项扩展IdentityUser类型(在本例中为Guid) ,这将需要将其余的通用实体类型扩展为用户的Guid键。这样做great article - 他使用一个整数,但相同的模式将用于guid键。

或者,您可以将解析字符串Id的用户实体上的计算属性公开为guid,因为这是