【发布时间】:2018-12-10 23:49:53
【问题描述】:
我正在学习 Asp.net Core 和 Entity Framework Core,并使用代码优先方法创建了一个基本的 Web 应用程序。我的应用程序有一个上下文 (AppContext),为了使用 Identity,我定义了一个新的上下文 (IdentityContext),它使用相同的数据库。 我想将每个身份的用户(来自 IdentityContext 的 AppUser)与一个员工(来自 AppContext)相关联,但是当我尝试它时,EF Core 会为它创建一个新表。
public class AppUser : IdentityUser
{
public Employee Employee { get; set; }
}
public class Employee
{
public long Id { get; set; }
public string AspNetUserId { get; set; }
public AppUser AppUser { get; set; }
}
应用上下文
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>(employee =>
{
employee.HasIndex(e => e.AspNetUserId);
employee
.HasOne(e => e.AppUser)
.WithOne(au => au.Employee)
.HasForeignKey<Employee>(e=>e.AspNetUserId);
});
}
我对此有很多疑问:
- 我认为有两个独立的上下文是可以的,每个上下文只有一个关注点。可以吗?还是应该将我的 AppContext 复制粘贴到 IdentityContext 上?
- 如何告诉我的 AppContext 没有为 AppUser 创建新表,而是使用 IdentityContext 创建的表?
- 还有其他选择吗?
任何建议或参考将不胜感激。
谢谢。
【问题讨论】:
标签: asp.net-identity entity-framework-core