2014-03-07 56 views
0

使用实体框架处理我的第一个MVC5项目。当我加载用户并尝试访问他们所在的角色时,延迟加载正尝试从AspNetUserAspNetRoles加载。该表实际上命名为AspNetUserRoles,我认为它是默认的,并且在向用户添加角色时工作正常。我的DBA为我设置了最初的模型集合,并且正在参加一个会议。我想我可能会在我的DBContext中丢失一些东西,但一小时后我仍然在寻找一个很好的参考来解决我的问题。无效的对象名称'dbo.AspNetUserAspNetRoles'

这是我的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

好了...想通了什么是可能处理这个问题的上级方法。我创建名为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()的映射文件I加入下面包含它。

modelBuilder.Configurations.Add(new AspNetRoleMap()); 
+0

请编辑您的previos答案并发布整个解决方案并删除此答案,以使其他人更容易理解解决方案。如果它解决了您的问题,请将您的答案标记为已接受。 –

+0

相反,我删除了以前,因为这是整个解决方案。直到48小时后,我才能将其标记为已接受。 – Tom