Question

Working with my first MVC5 project using entity framework. When I load my users and attempt access the roles they are in, the lazy load is trying to load from AspNetUserAspNetRoles. The table is actually named AspNetUserRoles which I think is the default and is working fine when I am adding roles to a user. My DBA setup the initial model collection for me and is away at a conference. I think I am likely missing something in my DBContext, but after an hour I am still looking for a good reference to solve my issue.

Here is my AspNetUser model. I eliminated a few of the other relationships to conserve space

    public partial class AspNetUser : IBusinessObject
{
    public AspNetUser()
    {
        this.AspNetRoles = new List<AspNetRole>();
    }

    public string Id { get; set; }
    public string UserName { get; set; }
    public string PasswordHash { get; set; }
    public string SecurityStamp { get; set; }
    public string Discriminator { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public System.DateTime CreatedOn { get; set; }
    public System.DateTime UpdatedOn { get; set; }
    public string CreatedByUserId { get; set; }
    public bool IsDeleted { get; set; }
    public string ResetPasswordKey { get; set; }
    public Nullable<System.DateTime> ResetPasswordKeyExpiration { get; set; }
    public virtual ICollection<AspNetRole> AspNetRoles { get; set; }

    public void SoftDelete(bool reverse = false)
    {
        IsDeleted = !reverse;
    }

}

Here is the AspNetRole model

public partial class AspNetRole : INameable, IBusinessObject
{
    public AspNetRole()
    {
        this.AspNetUsers = new List<AspNetUser>();
    }

    public string Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<AspNetUser> AspNetUsers { get; set; }

}

Here is my DbContext

public partial class SdContext : DbContext
    {
        static SdContext()
        {
            Database.SetInitializer<SdContext>(null);
        }

        public SdContext()
            : base("SdConnection")
        {
        }

        public DbSet<Cluster> Clusters { get; set; }
        public DbSet<CoLabDate> CoLabDates { get; set; }
        public DbSet<CoLabLocation> CoLabLocations { get; set; }
        public DbSet<CoLab> CoLabs { get; set; }
        public DbSet<CoLabType> CoLabTypes { get; set; }
        public DbSet<Contact> Contacts { get; set; }
        public DbSet<Document> Documents { get; set; }
        public DbSet<Organization> Organizations { get; set; }
        public DbSet<Participant> Participants { get; set; }
        public DbSet<Project> Projects { get; set; }
        public DbSet<Statement> Statements { get; set; }
        public DbSet<TriggeringQuestion> TriggeringQuestions { get; set; }
        public DbSet<VoteAction> VoteActions { get; set; }
        public DbSet<Vote> Votes { get; set; }
        public DbSet<VoteType> VoteTypes { get; set; }
        public DbSet<AspNetUser> AspNetUser { get; set; }
        public DbSet<AspNetRole> AspNetRole { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new ClusterMap());
            modelBuilder.Configurations.Add(new CoLabDateMap());
            modelBuilder.Configurations.Add(new CoLabLocationMap());
            modelBuilder.Configurations.Add(new CoLabMap());
            modelBuilder.Configurations.Add(new CoLabTypeMap());
            modelBuilder.Configurations.Add(new ContactMap());
            modelBuilder.Configurations.Add(new DocumentMap());
            modelBuilder.Configurations.Add(new OrganizationMap());
            modelBuilder.Configurations.Add(new ParticipantMap());
            modelBuilder.Configurations.Add(new ProjectMap());
            modelBuilder.Configurations.Add(new StatementMap());
            modelBuilder.Configurations.Add(new TriggeringQuestionMap());
            modelBuilder.Configurations.Add(new VoteActionMap());
            modelBuilder.Configurations.Add(new VoteMap());
            modelBuilder.Configurations.Add(new VoteTypeMap());
        }
    }
Was it helpful?

Solution

Okay...figured out what is probably the superior way to handle this. I created a mapping file named AspNetRoleMap.cs

public AspNetRoleMap()
{
    // Primary Key
    this.HasKey(t => t.Id);

    // Properties
    // Table & Column Mappings
    this.ToTable("AspNetRoles");
    this.Property(t => t.Id).HasColumnName("Id");
    this.Property(t => t.Name).HasColumnName("Name");

    // Relationships
    this.HasMany(u => u.AspNetUsers)
        .WithMany(t => t.AspNetRoles)
        .Map(
            m =>
            {
                m.MapLeftKey("RoleId");
                m.MapRightKey("UserId");
                m.ToTable("AspNetUserRoles");
            }
        );

}

Then in the DbContext OnModelCreating() I added the below to include it.

modelBuilder.Configurations.Add(new AspNetRoleMap());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top