继承ApplicationUser会引发验证错误

时间:2016-08-12 12:50:13

标签: asp.net entity-framework asp.net-identity

我对下面代码的意图是继承ApplicationUser并实现一种名为Customer的新用户。特定于此用户的信息需要保存在名为“Customers”的单独表中 下面是使用的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中有Q&amp; As,它说要修复OnModelCreating方法,但是这个项目没有这样的方法。

2 个答案:

答案 0 :(得分:3)

Package Manager Console抛出验证错误的原因是因为最初通过配置,EF正在读取除ApplicationDbContext之外的DbContext。以下是我解决问题的方法,

  1. 删除与ApplicationDbContext相关的所有表格
  2. 将以下命令发送给程序包管理器。

    1. Enable-Migrations -Force -ContextTypeName "ETrading.Models.ApplicationDbContext"
    2. 这会将迁移配置更改为使用ApplicationDbContext作为DbContext

      1. Add-Migration init
      2. update-database -force -verbose
      3. 创建初始显式迁移代码并更新ApplicationDbContext类中提供的目标数据库。

        当我们想要更改继承自ApplicationUser的模型对象时,我们可以简单地使用console Add-Migration and Update-Database命令而不删除已创建的表。

答案 1 :(得分:0)

你实际上需要以相反的方式做到这一点。您需要将客户添加到您的应用程序用户。所以你这样做

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;}
}
相关问题