2016-08-12 74 views
0

我的意圖是下面的代碼繼承ApplicationUser並實現一種新的名爲Customer的用戶。具體到該用戶的信息需要被保存在一個名爲「客戶」 下面是ApplicationUser,客戶和ApplicationDbContext類一個單獨的表是用來繼承ApplicationUser拋出驗證錯誤

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

應用建立正常,但當我發出的添加,遷移的遷移命令包管理器控制檯,我得到這個錯誤,

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. 

我看到有Q &如#1,它說修復OnModelCreating方法,但這個項目不具備這樣的方法。

回答

2

之所以軟件包管理器控制檯被扔驗證錯誤做是因爲最初的配置,EF在讀比ApplicationDbContext以外的DbContext。下面是我如何解決了這個問題,

  1. 刪除與ApplicationDbContext

發送下面的命令來包管理器中的所有表。

  • Enable-Migrations -Force -ContextTypeName "ETrading.Models.ApplicationDbContext"
  • 這改變了遷移配置爲使用ApplicationDbContext作爲的DbContext

  • Add-Migration init
  • update-database -force -verbose
  • 創建初始顯式遷移代碼和u修改ApplicationDbContext類中提供的目標數據庫。

    當我們想更改模型對象從ApplicationUser繼承我們可以簡單地使用控制檯Add-Migration and Update-Database命令而不刪除已經創建的表。

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