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