
[英]The entity type ApplicationUser is not part of the model for the current context
[英]The entity type ApplicationUser is not part of the model for the current context
我将根据本文从Identity 1.0.0迁移到Identity 2.0.1
并且生成的迁移代码与新的IdentityUser无关。 它不会添加新列。
因此,我做了一个新项目,然后再次尝试,但是迁移代码为空。
为了解决该问题,我直接在SQL Server中进行了编辑,然后在解决方案中再次导入了数据库。
现在,您可以看到,我的AspNetUser
与我的IdentityUser
完全相同
身份用户
public virtual int AccessFailedCount { get; set; }
public virtual ICollection<TClaim> Claims { get; }
public virtual string Email { get; set; }
public virtual bool EmailConfirmed { get; set; }
public virtual TKey Id { get; set; }
public virtual bool LockoutEnabled { get; set; }
public virtual DateTime? LockoutEndDateUtc { get; set; }
public virtual ICollection<TLogin> Logins { get; }
public virtual string PasswordHash { get; set; }
public virtual string PhoneNumber { get; set; }
public virtual bool PhoneNumberConfirmed { get; set; }
public virtual ICollection<TRole> Roles { get; }
public virtual string SecurityStamp { get; set; }
public virtual bool TwoFactorEnabled { get; set; }
public virtual string UserName { get; set; }
IdentityUser.cs
public class ApplicationUser : IdentityUser
{
public bool Has_accepted_policy { get; set; }
public int user_type_id { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
}
AspNetUser
public string Id { get; set; }
[Required]
[StringLength(256)]
public string UserName { get; set; }
public string PasswordHash { get; set; }
public string SecurityStamp { get; set; }
[StringLength(256)]
public string Email { get; set; }
public bool EmailConfirmed { get; set; }
public bool Is_Active { get; set; }
[Required]
[StringLength(128)]
public string Discriminator { get; set; }
public int? user_type_id { get; set; }
public bool Has_accepted_policy { get; set; }
public string PhoneNumber { get; set; }
public bool PhoneNumberConfirmed { get; set; }
public bool TwoFactorEnabled { get; set; }
public DateTime? LockoutEndDateUtc { get; set; }
public bool LockoutEnabled { get; set; }
public int AccessFailedCount { get; set; }
... other virtual properties
当我尝试注册用户时,出现以下异常
实体类型ApplicationUser不是当前上下文模型的一部分
在这条线
IdentityResult result = await UserManager.CreateAsync(user, model.Password);
我的startup.Auth.cs
UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
在我的AccountController中,我这样声明UserManager
public AccountController()
: this(Startup.UserManagerFactory(), Startup.OAuthOptions.AccessTokenFormat)
{
}
public AccountController(UserManager<ApplicationUser> userManager,
ISecureDataFormat<AuthenticationTicket> accessTokenFormat)
{
UserManager = userManager;
AccessTokenFormat = accessTokenFormat;
}
public UserManager<ApplicationUser> UserManager { get; private set; }
除了AspNetUser
类中的新属性外,我没有进行任何其他更改,并且它在迁移之前可以正常工作。
CodePlex上有一个类似的问题,标记为固定,但没有给出解决方案
有谁知道如何解决这一问题?
编辑
为了确保在编辑SQL数据库时我没有犯任何错误。 我创建了另一个项目并生成了一个身份数据库,并且更改了该数据库的连接字符串,但仍然遇到相同的错误。
解
当我已经编辑我的数据库我还没有注意到,在身份2.0.0他们改变了User_Id
的UserId
在AspUserClaims
表。 完成此操作后,我遇到了相同的错误,但随后我做了tschmit007所说的关于将ApplicationDbContext
添加到UserStore
构造函数的操作,现在它可以工作了。
UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
我遇到了同样的问题。 我正在使用EDMX文件进行数据库首次开发。
如果您使用的是在:base(“EDMXConnString”)
添加EDMX文件时生成的连接字符串,则很可能会出现此问题。
我通过创建一个指向ASP.NET Identity表所在数据库的标准连接字符串来解决此问题。
<add name="MyConnString" connectionString="Data Source=server; Initial Catalog=db_name; User ID=user_id; Password=password; Connect Timeout=60;" providerName="System.Data.SqlClient" />
然后在:base
使用该连接字符串,就可以了!
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("MyConnString")
{
}
}
对我来说,似乎错过了上下文说明:
UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
应该
UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
我的问题是我尝试将生成的ADO.NET连接字符串用于生成的上下文和身份验证上下文ApplicationDbContext
。 我通过使用单独的连接字符串进行身份验证来修复它。 还请注意提供者-对于身份验证上下文,它必须是System.Data.SqlClient
:
<add name="DefaultConnection" connectionString="Server=qadb.myserver.com;Database=mydb;User Id=myuser;Password=mypass;" providerName="System.Data.SqlClient" />
如果首先使用代码,请检查您的连接字符串以确保providerName为'SqlClient',如providerName =“ System.Data.SqlClient
如果首先使用数据库,请检查连接字符串以确保providerName为'EntityClient',如providerName =“ System.Data.EntityClient
我也收到了此错误消息,但是原因和解决方法不同。 就我而言,我在ApplicationUser类中引入了Guid类型的新Id属性。 完全有效的C#语法,但显然对Identity或EntityFramework核心(依赖于反射来查找内容)造成了极大的混乱。
在我的ApplicationUser类中删除新的Id属性可以解决此错误。
我遇到了这个问题,这是一个对象名称冲突。 IdentityConfig.cs使用的是ApplicationUser,但使用的是自动生成的IdentityModels.ApplicationUser,而不是我自己上下文的DataAccess.ApplicationUser 。 一旦找到,便说得很对。 因此,我从基本的WebAPI模板中删除了自动生成的IdentityModels.cs-不再使用它了-然后,我在IdentityConfig.cs中将using语句添加到了自己的DataAccess命名空间中,并确定了正确的映射。 如果您忘记了为您构建了大量模板的模板,则会遇到问题:
public class ApplicationUserManager : UserManager<ApplicationUser> // the name conflict
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
对我来说同样的问题,它可以通过以下代码解决:
public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false)
{
Database.Connection.ConnectionString = @"data source=...;initial catalog=...;user id=...;password=...;multipleactiveresultsets=True;application name=EntityFramework";
}
我的问题是我创建了一个新的DbContext,但它不是从IdentityDbContext继承的。
一个简单的解决方案...
public partial class GoldfishDbContext : IdentityDbContext<ApplicationUser>
{
....
}
我确定这不是为什么会发生,我的解决方案工作得很好,在入睡之前测试了所有内容。 12小时后,我再次检查并运行,这是完全相同的错误。 我在SO中尝试了几乎所有解决方案,但没有一个起作用。
我在这里实现数据库方法。 然后突然有这个
默认连接
在我第一次创建解决方案时Visual Studio生成的web.config中。 因此,我用它代替了我的EDMX文件生成的连接字符串,然后突然起作用了!
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
这是我的连接字符串,可以正常工作:
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-System.WEB-20180718085411.mdf;Initial Catalog=aspnet-System.WEB-20180718085411;Integrated Security=True" providerName="System.Data.SqlClient" />
最初,我使用的是由我的EDMX文件生成的文件,但尽管以前可以正常使用,但该网站突然无法工作。 我没有做任何更改,所有代码都在TFS中,所以我100%确信它可以工作,并且进行了完整还原并获得了最新版本:
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-System.WEB-20180718085411.mdf;Initial Catalog=aspnet-System.WEB-20180718085411;Integrated Security=True" providerName="System.Data.SqlClient" />
这发生在我身上,是因为我试图使用依赖注入容器连接ApplicationUserManager和其他一些相关的依赖。 在某些情况下,容器解析了ApplicationDbContext;在其他情况下,Owin中的内置注入器将解析它。
确保不发生这种情况的最简单方法是,除非您真的知道您对DI所做的事情,否则不要尝试使用您选择的DI容器连接任何Auth内容……否则,请让Owin使用内置的在喷油器中。
换句话说,删除类似的内容:
builder.RegisterType<ApplicationUserManager>().InstancePerRequest();
然后让Owin用内置的方法来解决它:
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.