使用实体框架处理我的第一个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());
}
}
好吧...找出可能是解决此问题的最佳方法。我创建了一个名为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());
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句