Mar*_*arc 45 c# asp.net entity-framework asp.net-identity
我将在本文后面从Identity 1.0.0迁移到Identity 2.0.1
生成的迁移代码与新的IdentityUser无关.它不会添加新列.
所以我创建了一个新项目并再次尝试,但迁移代码为空.
为了解决这个问题,我直接在SQL Server中进行了编辑,并在我的解决方案中再次导入了我的数据库.
现在我和你所看到的AspNetUser
完全一样IdentityUser
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; }
Run Code Online (Sandbox Code Playgroud)
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")
{
}
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
当我尝试注册用户时,我有以下异常
实体类型ApplicationUser不是当前上下文的模型的一部分
在这条线上
IdentityResult result = await UserManager.CreateAsync(user, model.Password);
Run Code Online (Sandbox Code Playgroud)
我的startup.Auth.cs
UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
Run Code Online (Sandbox Code Playgroud)
在我的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; }
Run Code Online (Sandbox Code Playgroud)
除了类中的新属性之外,我没有更改任何内容,AspNetUser
并且它在迁移之前一直运行良好.
CodePlex上有一个类似的问题标记为已修复,但它们没有给出解决方案
有谁知道如何解决这一问题?
编辑
当我编辑我的SQL数据库时,确保我没有犯任何错误.我创建了另一个项目并生成了一个Identity数据库,我更改了该数据库的连接字符串,但仍然有同样的错误.
解
当我已经编辑我的数据库我还没有注意到,在身份2.0.0他们改变了User_Id
对UserId
在AspUserClaims
表中.在这之后我有同样的错误,但后来我做了什么tschmit007说关于添加ApplicationDbContext
到UserStore
构造函数,现在它的工作原理.
UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
Run Code Online (Sandbox Code Playgroud)
Jam*_*mes 70
我遇到了同样的问题.我正在使用EDMX文件进行数据库优先开发.
如果您正在使用添加EDMX文件时生成的连接字符串,:base(“EDMXConnString”)
则很可能会出现此问题.
我通过创建一个标准连接字符串来修复此问题,该字符串指向ASP.NET标识表所在的数据库.
<add name="MyConnString" connectionString="Data Source=server; Initial Catalog=db_name; User ID=user_id; Password=password; Connect Timeout=60;" providerName="System.Data.SqlClient" />
Run Code Online (Sandbox Code Playgroud)
然后使用该连接字符串:base
,它工作了!
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("MyConnString")
{
}
}
Run Code Online (Sandbox Code Playgroud)
tsc*_*007 41
对我来说似乎错过了一个上下文实例:
UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
Run Code Online (Sandbox Code Playgroud)
应该
UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
Run Code Online (Sandbox Code Playgroud)
小智 7
我的问题是我尝试将生成的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" />
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
69678 次 |
最近记录: |