0

I am using Entity Framework with the Code First approach. I created a MVC app and 'registered' and it generated all of the AspNet tables.

I would like to have a AspNetUsers model so one of my other models can reference it. I have tried adding a mapping like the below but I am still getting the There is already an object named 'AspNetUsers' in the database. error.

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; }
    }

Mapping

 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>();
        }

UPDATE:

I think I am asking the question in the wrong way.

Basically how can I do this:

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; }
}
Jamie Rees
  • 7,973
  • 2
  • 45
  • 83
  • You don't need DbSet for ApsNetUser - that is handled by IdentityDbContext. I've had no issues customizing my derived class. – Steve Greene May 21 '15 at 20:02
  • You should do something like: public class ApplicationUser : IdentityUser { } http://www.codeproject.com/Articles/790720/ASP-NET-Identity-Customizing-Users-and-Roles – Steve Greene May 21 '15 at 20:09
  • The easiest thing would probably be to derive your DbContext from IdentityDbContext where ApplicationUser derives from IdentityUser. http://stackoverflow.com/questions/20104289/foreign-key-to-microsoft-aspnet-identity-entityframework-identityuser – Steve Greene May 21 '15 at 20:51

1 Answers1

-1

I have not had any success customizing the schema for the identity entity using code first. I ended up modifying the migration code that EF generated instead, and just make sure not to run add-migration Initial -force without doing a careful merge. This also allowed me to change the clustered index to a datetime2 column, getting around the horrible performance problems of keeping a clustered index on a guid.

Edit: If you are trying to change the type of the primary key for the user entity, you'll need to extend the IdentityUser type with your key choice (in this case Guid), which will then necessitate extending the rest of the generic entity types to Guid keys for the user as well. There's a great article on doing this - he uses an integer, but the same pattern would be used for a guid key.

Alternatively, you could expose a computed property on the user entity that parsed the string Id into a guid, since that is

Community
  • 1
  • 1
RyanR
  • 7,728
  • 1
  • 25
  • 39