【问题标题】:Inheriting ApplicationUser throws validation errors继承 ApplicationUser 引发验证错误
【发布时间】: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


    【解决方案1】:

    包管理器控制台抛出验证错误的原因是,最初通过配置,EF 正在读取除 ApplicationDbContext 之外的 DbContext。以下是我解决问题的方法,

    1. 删除所有与ApplicationDbContext相关的表

    将以下命令发送到包管理器。

    1. Enable-Migrations -Force -ContextTypeName "ETrading.Models.ApplicationDbContext"

    这会将迁移配置更改为使用 ApplicationDbContext 作为 DbContext

    1. Add-Migration init
    2. update-database -force -verbose

    创建初始显式迁移代码并更新 ApplicationDbContext 类中提供的目标数据库。

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

    【讨论】:

      【解决方案2】:

      你实际上需要反过来做。您需要将客户添加到您的应用程序用户。所以你做如下

      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;}
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-21
        • 1970-01-01
        相关资源
        最近更新 更多