2017-08-28 3 views
2

私はそうのようなデフォルトのasp.netのコアアイデンティティユーザを拡張していますEFからの読み出しアイデンティティユーザー情報に

public class ApplicationUser : IdentityUser 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public MyObject SomeObject { get; set; } 
    public List<Email> AssociatedEmails { get; set; } 
} 

すべてのユーザーデータがAspNetusersと呼ばれるテーブルに格納されているようですが、そこ私のコードでそれを参照することはできませんので、ApplicationDbContextでこれについての定義はありません。

私はEntity Frameworkからさまざまなユーザーの詳細を読み取ることができ、ユーザーレコードをさまざまなエンティティに関連付ける必要があるため、EFを通じてユーザーエンティティにアクセスできる必要があります。

これを行う適切な方法は何ですか?

答えて

0

ApplicationDbContextIdentityDbContext<ApplicationUser>で、あなた自身のIdentityDbContext

public class YourApplicationDbContext : IdentityDbContext<YourApplicationUser> 
{ 
    public YourApplicationDbContext (DbContextOptions options) : base(options) { } 

    protected override void OnModelCreating(ModelBuilder builder) 
    { 
     base.OnModelCreating(builder); 
     // Customize the ASP.NET Identity model and override the defaults if needed. 
     // For example, you can rename the ASP.NET Identity table names and more. 
     // Add your customizations after calling base.OnModelCreating(builder); 
    } 
} 

を書いて、あなたの起動クラスで

services.AddDbContext<YourApplicationDbContext>(options => 
       options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); 

services.AddIdentity<YourApplicationUser, IdentityRole>() 
       .AddEntityFrameworkStores<YourApplicationDbContext>() 
       .AddDefaultTokenProviders(); 
を使用することができます