2016-08-12 13 views
0

以下のコードでは、ApplicationUserを継承し、Customerという新しい種類のユーザーを実装します。このユーザに固有の情報は、アプリケーションが正常にビルドを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; } 
    } 

使用されている「お客様」 は以下ApplicationUser、顧客であり、ApplicationDbContextクラスと呼ばれる別のテーブルに保存する必要がありますが、私は追加-移行移行コマンドからを発行したときパッケージマネージャコンソール、私はこのエラーを取得し、

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 &は、それがOnModelCreatingメソッドを修正するために言いますが、このプロジェクトは、このような方法を持っていないのStackOverflowのようにありました見ました。

答えて

2

理由を次のようにしてください。以下は、私が

  1. がマネージャーをパッケージ化するためのコマンドを以下のApplicationDbContext

送信に関連するすべてのテーブルを削除し、問題を解決する方法です。

これはDbContextとしてApplicationDbContextを使用する移行設定を変更
  • Enable-Migrations -Force -ContextTypeName "ETrading.Models.ApplicationDbContext"
  • Add-Migration init
  • update-database -force -verbose
  • 最初の明示的な移行コードを作成し、 ApplicationDbContextクラスで提供されるターゲットデータベースをpdateします。

    モデルオブジェクトを変更する場合ApplicationUserから継承した場合、コンソールAdd-Migration and Update-Databaseコマンドを使用して、作成済みのテーブルを削除せずに済みます。

    0

    実際には逆にする必要があります。顧客をアプリケーション・ユーザーに追加する必要があります。当初の設定によって、EFがApplicationDbContext以外のDbContextを読んでいたので、だから、パッケージマネージャコンソールが検証エラーを投げた理由

    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;} 
    } 
    
    関連する問題