C# 无效的对象名称';dbo.AspNetUserAspNetRoles';
使用实体框架处理我的第一个MVC5项目。当我加载我的用户并尝试访问他们所处的角色时,延迟加载正在尝试从AspNetUserAspNetRoles加载。该表实际上名为AspNetUserRoles,我认为这是默认值,在向用户添加角色时工作正常。我的DBA为我设置了初始模型集合,并且正在参加一个会议。我想我可能在我的DBContext中遗漏了一些东西,但一个小时后,我仍然在寻找一个好的参考来解决我的问题 这是我的AspNetUser模型。为了节省空间,我消除了一些其他关系C# 无效的对象名称';dbo.AspNetUserAspNetRoles';,c#,asp.net-mvc,entity-framework,identity,C#,Asp.net Mvc,Entity Framework,Identity,使用实体框架处理我的第一个MVC5项目。当我加载我的用户并尝试访问他们所处的角色时,延迟加载正在尝试从AspNetUserAspNetRoles加载。该表实际上名为AspNetUserRoles,我认为这是默认值,在向用户添加角色时工作正常。我的DBA为我设置了初始模型集合,并且正在参加一个会议。我想我可能在我的DBContext中遗漏了一些东西,但一个小时后,我仍然在寻找一个好的参考来解决我的问题 这是我的AspNetUser模型。为了节省空间,我消除了一些其他关系 public pa
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;
}
}
公共部分类AspNetUser:IBusinessObject
{
公共AspNetUser()
{
this.AspNetRoles=新列表();
}
公共字符串Id{get;set;}
公共字符串用户名{get;set;}
公共字符串密码哈希{get;set;}
公共字符串SecurityStamp{get;set;}
公共字符串鉴别器{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串电子邮件{get;set;}
public System.DateTime CreatedOn{get;set;}
public System.DateTime UpdatedOn{get;set;}
公共字符串CreatedByUserId{get;set;}
公共布尔被删除{get;set;}
公共字符串ResetPasswordKey{get;set;}
公共可为空的ResetPasswordKeyExpiration{get;set;}
公共虚拟ICollection AspNetRoles{get;set;}
public void SoftDelete(bool reverse=false)
{
IsDeleted=!反向;
}
}
以下是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; }
}
公共部分类AspNetRole:不可测量,IBusinessObject
{
公共AspNetRole()
{
this.AspNetUsers=新列表();
}
公共字符串Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection 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());
}
}
公共部分类SdContext:DbContext
{
静态SdContext()
{
Database.SetInitializer(null);
}
公共上下文()
:基本(“SdConnection”)
{
}
公共数据库集群集{get;set;}
公共DbSet CoLabDates{get;set;}
公共DbSet CoLabLocations{get;set;}
公共DbSet CoLabs{get;set;}
公共DbSet CoLabTypes{get;set;}
公共数据库集联系人{get;set;}
公共数据库集文档{get;set;}
公共数据库集组织{get;set;}
公共数据库集参与者{get;set;}
公共数据库集项目{get;set;}
公共DbSet语句{get;set;}
公共数据库集触发问题{get;set;}
公共DbSet VoteActions{get;set;}
公共数据库集投票{get;set;}
公共DbSet VoteTypes{get;set;}
公共DbSet AspNetUser{get;set;}
公共DbSet AspNetRole{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
添加(新的ClusterMap());
添加(新的CoLabDateMap());
添加(新的CoLabLocationMap());
添加(新的CoLabMap());
添加(新的CoLabTypeMap());
添加(新的ContactMap());
modelBuilder.Configurations.Add(newdocumentmap());
添加(新的OrganizationMap());
modelBuilder.Configurations.Add(newparticipantmap());
添加(新的ProjectMap());
添加(newstatementmap());
添加(新的TriggeringQuestionMap());
添加(新的VoteActionMap());
添加(新的VoteMap());
添加(新的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());
请编辑您的previos答案,并将整个解决方案张贴在那里,然后删除此答案,以便其他人更容易理解解决方案是什么。如果它解决了您的问题,请将您的答案标记为已接受。相反,我删除了前面的答案,因为这是整个解决方案。我不能将其标记为已接受,直到它已运行48小时。