Asp.net mvc 5 上下文错误-实体类型ApplicationUser不是当前上下文的模型的一部分
嗯,我在没有任何帮助的情况下尝试了很多事情。 这种情况是,如果我将我的连接字符串作为常规连接字符串,例如:Asp.net mvc 5 上下文错误-实体类型ApplicationUser不是当前上下文的模型的一部分,asp.net-mvc-5,entity-framework-6,asp.net-identity,owin,Asp.net Mvc 5,Entity Framework 6,Asp.net Identity,Owin,嗯,我在没有任何帮助的情况下尝试了很多事情。 这种情况是,如果我将我的连接字符串作为常规连接字符串,例如: <add name="MyConnectionString" connectionString="Data Source=mydatasource;Connection Lifetime=10;Max Pool Size=800;Initial Catalog=mydb;Persist Security Info=True;MultipleActiveResultSets=True;
<add name="MyConnectionString" connectionString="Data Source=mydatasource;Connection Lifetime=10;Max Pool Size=800;Initial Catalog=mydb;Persist Security Info=True;MultipleActiveResultSets=True;User ID=myuser;Password=mypass" providerName="System.Data.SqlClient" />
错误来自这里:
public partial class myConnectionString : DbContext
{
public myConnectionString()
: base("name=myConnectionString")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();// error comes from here
}
public virtual DbSet<AspNetRoles> AspNetRoles { get; set; }
public virtual DbSet<AspNetUserClaims> AspNetUserClaims { get; set; }
public virtual DbSet<AspNetUserLogins> AspNetUserLogins { get; set; }
public virtual DbSet<AspNetUsers> AspNetUsers { get; set; }
public virtual DbSet<ConnectedUsers> ConnectedUsers { get; set; }
}
var user = UserManager.Find(email, password);
错误来自这里:
public partial class myConnectionString : DbContext
{
public myConnectionString()
: base("name=myConnectionString")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();// error comes from here
}
public virtual DbSet<AspNetRoles> AspNetRoles { get; set; }
public virtual DbSet<AspNetUserClaims> AspNetUserClaims { get; set; }
public virtual DbSet<AspNetUserLogins> AspNetUserLogins { get; set; }
public virtual DbSet<AspNetUsers> AspNetUsers { get; set; }
public virtual DbSet<ConnectedUsers> ConnectedUsers { get; set; }
}
var user = UserManager.Find(email, password);
这是我在startup.auth.cs中的内容
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context and user manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}
public void ConfigureAuth(IAppBuilder应用程序)
{
//将db上下文和用户管理器配置为每个请求使用一个实例
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext(ApplicationUserManager.Create);
//使应用程序能够使用cookie存储登录用户的信息
//以及使用cookie临时存储用户登录第三方登录提供商的信息
//配置登录cookie
app.UseCookieAuthentication(新的CookieAuthenticationOptions
{
AuthenticationType=DefaultAuthenticationTypes.ApplicationOkie,
LoginPath=新路径字符串(“/Account/Login”),
Provider=新CookieAuthenticationProvider
{
OnValidateIdentity=SecurityStampValidator.OnValidateIdentity(
validateInterval:TimeSpan.FromMinutes(30),
regenerateIdentity:(管理器,用户)=>user.GenerateUserIdentityAsync(管理器))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}
需要注意的事项:
我在一个项目中有实体框架(EDMX),在另一个项目中有身份验证(IdentityModel,Usermanager)。我从另一个MVC5项目中使用了它(在那里我有startup.auth.cs)。
在app/web配置的所有项目中,我都有相同的连接字符串
我该如何解决这个问题?任何帮助 不能在一个LINQ查询中混合来自两个不同上下文的实体。但我不会这么做!!很公平,但我不知道你是干什么的。异常发生在哪里?(方法+行)更新了我的问题,其中错误来自于两个问题完全无关的特定点,第一个问题不再是问题。所以你最好把所有多余的文字从你的问题中删除。对于剩下的问题:如何实例化
UserManager
?
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context and user manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}