13

MVC5アプリケーションにプロファイル/メンバーシップ情報を追加し、構成マッピングを追加しようとしています。asp.net MVC5 EF6で流暢なAPIを使用してマップテーブルですか?

私は、次のエラーメッセージが表示されます。

my.Models.IdentityUserLogin:EntityType 'IdentityUserLoginは' 定義され キーを持っていません。このEntityTypeのキーを定義します。

my.Models.IdentityUserRole :: EntityType 'IdentityUserRole'にはキーがありません が定義されています。このEntityTypeのキーを定義します。

IdentityUserLogins:EntityType:EntitySet 'IdentityUserLogins'は、キーが定義されていないタイプ 'IdentityUserLogin'に基づいて です。

IdentityUserRoles:EntityType:EntitySet 'IdentityUserRoles'は、キーが定義されていないタイプ 'IdentityUserRole'の に基づいています。

public class ApplicationUser : IdentityUser 
{ 
    public string City { get; set; } 
    public string Discriminator { get; set; } 

    public string Address { get; set; }  
} 

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 
    public ApplicationDbContext() 
     : base("DefaultConnection") 
    { 
    } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Configurations.Add(new ApplicationUserConfiguration());   
    } 
} 

答えて

18

コールbase.OnModelCreating(modelBuilder)後に設定が追加されました。

+2

このdoesntのをwork..tryがそれを呼び出す場合コンフィグレーションの前に –

+1

'IdentityDbContext 'が使用され、 'OnModelCreating'がオーバーライドされ、' IdentityUsを手動で設定していない場合は 'base.OnModelCreating(modelBuilder);' erLogin'、 'IdentityUserRole'、' IdentityUserClaim'に属性や流暢なAPIを付けています。 –

22

base.OnModelCreating(modelBuilder)を呼び出すと、私の問題は解決しませんでした。

VS2013-Preview、VS2013-RC、およびVS2013-RTMでは、Microsoft.AspNet.Identity.EntityFrameworkの動作が異なるようです。私はRTMバージョンを使用しています。私はAspNet.Identity.EntityFrameworkに関する作業が進行中であると思い

public class ApplicationUser : IdentityUser 
{ 
    public string DisplayName { get; set; } 
} 


public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 
    public ApplicationDbContext() : base("DefaultConnection") { } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     base.OnModelCreating(modelBuilder); 
     modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId); 
     modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id); 
     modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId }); 
    } 

Configuring/Mapping Properties and Types with the Fluent APIを参照してください):

IdentityUserから継承した後、私はそれを動作させるために、モデル内の他のすべての主キーを再作成する必要がありましたこれは修正されます

+0

私はこの問題を解決するために全く同じことをしなければなりませんでした。 EFがエンティティIDを自動的に選ぶと思ったのですか? –

+2

これはすべて私のために解決しました!多くのおかげで - 答えがマークされる必要があります:) – Darren

+0

私は同様の問題を抱えていて、この解決策を試して問題を解決したと思っていますが、IdentityUsersなどのデータベースには余分なテーブルがいくつもあります。私は何を間違えたのですか? – lawphotog

0

は服用効果を認識するように各1の後OnModelCreating方法やテストの周りには、次の手順に従います(?):

  1. ルールの競合を避けるため、Contextがあることを確認してください。
  2. (最初のすべての)言及 メソッド内のコールbase.OnModelCreating(modelBuilder);
  3. 方法では以下のプラスのプレビューステップを追加します。


modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId); 
modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id); 
modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId }); 
関連する問題