1

EF 6と.NET 4.5に対してコードファーストを使用して、まったく新しい.NET MVCプロジェクトを開始します。私はIdentityフレームワークも使いたいし、自分のプロジェクトのコンテキスト設定内でIdentityベースクラスを拡張したい。 MVC、EF、アイデンティティに少し慣れている熟練したC#/ .NET開発者です。EFでの.NET IDクラスのインテグレーションと拡張(コードファースト)

私の問題:何かが間違っています。

POST.Data.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. 
POST.Data.Models.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. 

わかりましたので、私は私のプロジェクトに2つのコンテキスト、アイデンティティクラスに1つ、他のすべてのために他を持っている:新しい移行を生成しようと、私はエラーを取得しています。同じデータベースであり、いずれのコンテキストからも、他のものには何も言及されていません。私は以下のようなものをエンティティを定義します。次に

public class ApplicationUser : IdentityUser { } 
public class ApplicationRole : IdentityRole { } 
public class ApplicationUserRole : IdentityUserRole { } 
public class ApplicationUserClaim : IdentityUserClaim { } 
public class ApplicationUserLogin : IdentityUserLogin { } 

、私はこのような文脈のクラスがあります。それも問題

public partial class IdentityContext : IdentityDbContext 
{ 
    public IdentityContext() : base() { } 

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

    public DbSet<ApplicationUser> ApplicationUsers { get; set; } 
    public DbSet<ApplicationUserClaim> ApplicationUserClaims { get; set; } 
    public DbSet<ApplicationUserLogin> ApplicationUserLogins { get; set; } 
    public DbSet<ApplicationRole> ApplicationRoles { get; set; } 
    public DbSet<ApplicationUserRole> ApplicationUserRoles { get; set; } 
} 

わからないが、私の構成のものは、次のようになります。

internal sealed class Configuration : DbMigrationsConfiguration<POST.Data.Models.Context> 
{ 
    public Configuration() 
    { 
     AutomaticMigrationsEnabled = false; 
     MigrationsDirectory = @"ContextMigrations"; 
    } 

    protected override void Seed(POST.Data.Models.Context context) {} 
} 

それは構築されますが、そのようなエラーがなければ移行を生成することはできません。限られた知識とすべての詳細の理解で、私は困惑しています。

答えて

2

私はwouldn、役割などをユーザーのIDエンティティのいずれかを拡張していないので、独自のIDサブクラス(ApplicationUser、ApplicationRoleなど)を作成するのは面倒です。 IdentityDbContextから自動的に継承されるため、ID関連のDbSetsをコンテキストに定義する必要もありません。これはあなたのDbContextを自動的にせずに、関連するDbSetのすべてのプロパティを持っていることを確認します

public class IdentityContext : IdentityDbContext<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim> 
{ 
    // Define other non-identity related dbsets 
} 

:しかし、あなたのアイデンティティを維持したいならば、これはあなたがあなたのカスタム・アイデンティティ・エンティティを定義することができますようごDbContextは、一般的なIdentityDbContextから継承する必要がありますサブクラス一般的なIdentityDbContext <>クラスから継承しているときに明示的に定義する必要があります。

また、アイデンティティエンティティに何も追加していないため(たとえば、追加のApplicationUserプロパティ)、OnModelCreating内の任意のアイデンティティについて、移行や流暢なAPI設定を定義する必要はありません。デフォルトのIDテーブルは、DbContext/EFが初期化されるときに自動的に作成されます。

+0

これは私が取っていたオリジナルの方向ですが、将来私はそれらを拡張したいと思っていたので別のサブクラスを作成しました。しかし、私は正直に、それをただ残しているだけの論理を見ています。 –

+0

しかし、私が持っている質問と私はこれを正しく言いたいと思います。IdentityDbContextを継承し、代わりにDbContextを継承する別のコンテキストを持たない場合、ベースのIdentityクラスはどのように作成されますかそれらの具体的なエンティティが順番に私のデータベースに生成されるように、すべて?つまり、ある時点で、私はAspNet Identityテーブルを作成したいと言う必要があります。だから:どう?私はMainContextのような私の他のコンテキストを持っています:DbContext;それはMainContext:IdentityDbContextですか? –

+0

さて、大丈夫です。私はこれを理解していると信じています... :-Pあなたはちょうど別のコンテキストを作成し、それを空のままにします。 –

2

属性Keyを使用してエンティティキーを設定できます。このような何か:

using System.ComponentModel.DataAnnotations; 

namespace YourCompany.YourProject.Models 
{ 
    public class ApplicationUser : IdentityUser 
    { 
     [Key] 
     public string UserId { get; set; } 
    } 

    public class ApplicationRole : IdentityRole 
    { 
     [Key] 
     public string Id { get; set; } 
    } 

    public class ApplicationUserRole : IdentityUserRole 
    { 
     [Key] 
     public string UserId { get; set; } 

     [Key] 
     public string RoleId { get; set; } 
    } 

    public class ApplicationUserClaim : IdentityUserClaim { } 

    public class ApplicationUserLogin : IdentityUserLogin { } 
} 

私はそれはあなたと答えとしてそれがない場合は、pleseはマークそれを支援を期待;)

+0

ありがとうございました。私はこれを考えていましたが、基礎クラスにすでにマークされているものをキーと仮定していたので、試してみませんでした...これは私が受け取った初期のエラーが消えてしまったので、しかし、私は今隠しに関する警告を作成しています(オーバーライドを使用する必要がありますか?)、マイグレーションは引き続き作成されません: "アイデンティティー 'Id'を持つアイテムがメタデータコレクションに既に存在しています パラメータ名:item " –