【发布时间】:2016-08-12 12:50:12
【问题描述】:
我下面代码的意图是继承 ApplicationUser 并实现一种名为 Customer 的新用户。此用户的特定信息需要保存在名为“客户”的单独表中 下面是用到的 ApplicationUser、Customer 和 ApplicationDbContext 类
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; }
}
应用程序构建良好,但是当我从包管理器控制台发出 Add-Migration 迁移命令时,我收到此错误,
One or more validation errors were detected during model generation:
ETrading.DAL.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
ETrading.DAL.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.
我看到 Stackoverflow 中有问答,上面说要修复 OnModelCreating 方法,但是这个项目没有这样的方法。
【问题讨论】:
标签: asp.net entity-framework asp.net-identity