使用实体框架使用我的第一个MVC5项目。当我加载用户并尝试访问他们所在的角色时,延迟加载尝试从AspNetUserAspNetRoles加载。该表实际上名为AspNetUserRoles,我认为这是默认值,并且在我向用户添加角色时工作正常。我的DBA为我设置了初始模型集,并且正在参加会议。我想我可能在DBContext中遗漏了一些东西,但一小时后我仍然在寻找一个很好的参考来解决我的问题。
这是我的AspNetUser模型。我消除了一些其他关系以节省空间
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;
}
}
这是AspNetRole模型
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; }
}
这是我的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());
}
}
答案 0 :(得分:0)
好的......弄明白可能是处理这个问题的最好办法。我创建了一个名为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");
}
);
}
然后在DbContext OnModelCreating()中添加了以下内容以包含它。
modelBuilder.Configurations.Add(new AspNetRoleMap());