我在我的解決方案,域,API和Web中有3個項目。最近我更新了Asp.net Identity form 1.0到2.0。一切工作正常在我的Web項目,但是當我嘗試在網絡API項目獲得令牌,我得到這個錯誤(從1.0到2.0的一切升級Identity工作過):Asp.net身份2實體類型用戶不是當前上下文的模型的一部分
The entity type User is not part of the model for the current context
我的User類看起來是這樣的:
public class User : IdentityUser
{
//more code here
}
這裏是我的數據庫上下文類:
public class DatabaseContext : IdentityDbContext<User>
{
public DatabaseContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
Configuration.LazyLoadingEnabled = true;
}
//more code here
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>()
.ToTable("AspNetUsers");
modelBuilder.Entity<User>()
.ToTable("AspNetUsers");
}
}
在我的Web API項目,我已經更換了所有引用從IdentityUser到用戶,例如方法獲得令牌看起來像:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
using (UserManager<User> userManager = _userManagerFactory())
{
var user = userManager.Find(context.UserName, context.Password);
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
ClaimsIdentity oAuthIdentity = await userManager.CreateIdentityAsync(user,
context.Options.AuthenticationType);
ClaimsIdentity cookiesIdentity = await userManager.CreateIdentityAsync(user,
CookieAuthenticationDefaults.AuthenticationType);
AuthenticationProperties properties = CreateProperties(user.UserName);
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
context.Validated(ticket);
context.Request.Context.Authentication.SignIn(cookiesIdentity);
}
}
我該如何解決這個問題?
我認爲IdentityDbContext對於給定的用戶類型,所以我不知道這是正確的創建DbSet - 但嘗試'modelBuilder.Entity()...''而不是'modelBuilder.Entity ()...' –
我有兩個,但我刪除了modelBuilder.Entity(),只剩下modelBuilder.Entity (),但它沒有幫助 –
hyperN