【问题标题】:EF Code First: The entity type ApplicationUser is not part of the model for the current contextEF Code First:实体类型 ApplicationUser 不是当前上下文模型的一部分
【发布时间】:2016-12-25 19:44:04
【问题描述】:

我不知道为什么,但是当我尝试登录到我的 EF Code First MVC 应用程序时,我得到了这个错误。

我的家庭控制器有[Authorize] 属性,所以我被直接重定向到登录页面,但是一旦我登录,我就会得到黄屏死机和错误:

实体类型 ApplicationUser 不是当前上下文模型的一部分。

代码

这是我的上下文代码:

public class PdContext : IdentityDbContext<ApplicationUser>
{
  public PdContext() : base("PdContext", throwIfV1Schema: false)
  {
    Configuration.LazyLoadingEnabled = true;
  }

  public static PdContext Create()
  {
    return new PdContext();
  }

  public DbSet<Business> Businesses { get; set; }
  public DbSet<Category> Categories { get; set; }
  public DbSet<Brand> Brands { get; set; }
  public DbSet<Product> Products { get; set; }
  public DbSet<Address> Addresses { get; set; }
  public DbSet<Invoice> Invoices { get; set; }
  public DbSet<Payment> Payments { get; set; }
  public DbSet<Order> Orders { get; set; }

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    modelBuilder.HasDefaultSchema("PD");
    modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    base.OnModelCreating(modelBuilder);
    modelBuilder.Entity<IdentityRole>().HasKey(r => r.Id).ToTable("AspNetRoles");
    modelBuilder.Entity<IdentityUserLogin>().HasKey(l => new { l.UserId, l.LoginProvider, l.ProviderKey }).ToTable("AspNetUserLogins");
    modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId }).ToTable("AspNetUserRoles");
    modelBuilder.Entity<IdentityUserClaim>().ToTable("AspNetUserClaims");
    modelBuilder.Entity<ApplicationUser>().ToTable("User");
  }
}

这是我的 Seed 方法中负责创建我的用户和角色的代码。

protected override void Seed(Context.PdContext context)
{
  var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
  var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
  if (!roleManager.RoleExists("Admin"))
  {
    roleManager.Create(new IdentityRole("Admin"));
  }

  var adminUser = new ApplicationUser { UserName = "myemail@gmail.com", Email = "myemail@gmail.com" };

  if (userManager.FindByName("myemail@gmail.com") != null) return;

  var adminResult = userManager.Create(adminUser, "strongPassword");

  if (adminResult.Succeeded)
  {
    userManager.AddToRole(adminUser.Id, "Admin");
  }

  // other non user related seeding happening here
}

我的连接字符串如下:

<add name="PdContext" providerName="System.Data.SqlClient" connectionString="Data Source=My-PC;Initial Catalog=Phoneden;Integrated Security=False;User Id=sa;Password=strongPass;MultipleActiveResultSets=True" />

我检查过,我只有一个连接字符串(我对错误的研究表明可能有两个连接字符串,在我的情况下没有)。

更新

我禁用了[Authorize] 属性只是为了检查是否在数据库中创建了实际用户。事实证明,根本没有创建该用户!有什么想法吗?

【问题讨论】:

  • 那么,您是否只有一个具有身份所需表的数据库连接?
  • 是的。当我在 SQL Management Studio 中签入时,所有成员表都在那里!
  • 我已经为您的问题发布了一个潜在的解决方案。试一试,让我知道它是否适合您。
  • 您能否发布您如何构建上下文以便我们查看?
  • @DavidsonSousa 我已经添加了上下文代码

标签: c# asp.net-mvc entity-framework


【解决方案1】:

这可能是一个简单的解决方案,只需将连接字符串中的 Integrated Security 属性更改为 True。这说明要在提供的数据库中找到并使用身份表。

因此,您的连接字符串将是:

<add name="PdContext" providerName="System.Data.SqlClient" connectionString="Data Source=My-PC;Initial Catalog=Phoneden;Integrated Security=True;User Id=sa;Password=strongPass;MultipleActiveResultSets=True" />

【讨论】:

  • 我刚刚尝试了您的建议,但没有任何区别。我仍然遇到同样的错误。
【解决方案2】:

您的应用程序中可能有 2 个不同的上下文,这在我们从标准 ASP.NET MVC 模板开始时是正常的。我通常做的是删除标准上下文并适应我正在创建的一个(AppContext),如下面的代码所示:

public class AppContext : IdentityDbContext<ApplicationUser>
{
    public AppContext() : base("PdContext")
    {
    }
}

关键在于IdentityDbContext&lt;ApplicationUser&gt; 的继承,这使得成员资格在您的自定义上下文中工作。

【讨论】:

  • 我检查过,我的继承自IdentityDbContext&lt;ApplicationUser&gt;,我在原帖中添加了代码。
【解决方案3】:

我不知道为什么会这样,但我删除了我的 Migrations 文件夹,清理了我的解决方案,然后重新启用了 Migrations 并添加了 InitialCreate Migration。然后Update-Database 和现在似乎一切正常! :/

【讨论】:

    猜你喜欢
    • 2011-08-03
    • 2017-06-19
    • 2016-08-31
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多