Asp.net 继承ApplicationUser会引发验证错误
下面代码的目的是继承ApplicationUser并实现一种称为Customer的新用户。特定于此用户的信息需要保存在名为“Customers”的单独表中 下面是所使用的ApplicationUser、Customer和ApplicationDbContext类Asp.net 继承ApplicationUser会引发验证错误,asp.net,entity-framework,asp.net-identity,Asp.net,Entity Framework,Asp.net Identity,下面代码的目的是继承ApplicationUser并实现一种称为Customer的新用户。特定于此用户的信息需要保存在名为“Customers”的单独表中 下面是所使用的ApplicationUser、Customer和ApplicationDbContext类 public class ApplicationUser : IdentityUser { public async Task<ClaimsIdentity> GenerateUserIdentit
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("Data Source=.\\SQLEXPRESS;Initial Catalog=ETrading.DAL.DatabaseContext;Integrated Security=True", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
[Table("Customers")]
public class Customer : ApplicationUser
{
public int CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Status { get; set; }
public string BillingAddress { get; set; }
public string CustomerFullName
{get{
return (FirstName + " " + LastName);
} }
public string Remark { get; set; }
public ICollection<CustomerOrder> CustomerOrders { get; set; }
}
我看到Stackoverflow中有Q&As,它说要修复OnModelCreating方法,但这个项目没有这样的方法 实际上,你需要用另一种方式来做。您需要将客户添加到应用程序用户。所以你要做如下的事情
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
public virtual Customer customer {get;set;}
}
公共类应用程序用户:IdentityUser
{
公共异步任务GenerateUserIdentityAsync(用户管理器)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
公共虚拟客户客户{get;set;}
}
实际上,你需要用另一种方式来做。您需要将客户添加到应用程序用户。所以你要做如下的事情
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
public virtual Customer customer {get;set;}
}
公共类应用程序用户:IdentityUser
{
公共异步任务GenerateUserIdentityAsync(用户管理器)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
公共虚拟客户客户{get;set;}
}
Package Manager控制台引发验证错误的原因是,最初通过配置,EF读取的是ApplicationDbContext以外的DbContext。下面是我如何解决这个问题的
启用迁移-Force-ContextTypeName“etreading.Models.ApplicationDbContext”
addmigrationinit
updatedatabase-force-verbose
当我们想要更改从ApplicationUser继承的模型对象时,只需使用console
添加迁移和更新数据库
命令,而无需删除已创建的表 包管理器控制台引发验证错误的原因是,最初通过配置,EF读取的是ApplicationDbContext以外的DbContext。下面是我如何解决这个问题的
启用迁移-Force-ContextTypeName“etreading.Models.ApplicationDbContext”
addmigrationinit
updatedatabase-force-verbose
添加迁移和更新数据库
命令,而无需删除已创建的表