I have the following one-to-one relationship with ApplicationUser:
public class Person
{
public Guid PersonId { get; set; }
public string UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Company { get; set; }
public string Role { get; set; }
public string Country { get; set; }
public DateTime CreatedDate { get; set; }
public bool IsActive { get; set; }
public ApplicationUser User { get; set; }
}
public class ApplicationUser : IdentityUser
{
public Guid PersonId { get; set; }
public string Provider { get; set; } = "LOCAL";
public string ExternalUserId { get; set; }
public Person Person { get; set; }
}
DbContext:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> opts) : base(opts) { }
public DbSet<Person> Person { get; set; }
public DbSet<ApplicationUser> User { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new PersonConfiguration());
modelBuilder.Entity<ApplicationUser>(e => {
e.ToTable(name: "User");
e.HasOne(p => p.Person).WithOne(u => u.User);
//e.HasOne(p => p.Person).WithOne().HasForeignKey;
});
modelBuilder.Entity<IdentityRole>(e => e.ToTable(name: "Role"));
modelBuilder.Entity<IdentityUserRole<string>>(e => e.ToTable(name: "UserRole"));
modelBuilder.Entity<IdentityUserClaim<string>>(e => e.ToTable(name: "UserClaim"));
modelBuilder.Entity<IdentityUserLogin<string>>(e => e.ToTable(name: "UserLogin"));
modelBuilder.Entity<IdentityUserToken<string>>(e => e.ToTable(name: "UserToken"));
modelBuilder.Entity<IdentityRoleClaim<string>>(e => e.ToTable(name: "RoleClaim"));
}
}
Person entity configuration:
public class PersonConfiguration : IEntityTypeConfiguration<Person>
{
public void Configure(EntityTypeBuilder<Person> builder)
{
builder.ToTable("Person");
builder.HasOne(u => u.User)
.WithOne(p => p.Person)
.HasForeignKey<Person>(p => p.UserId);
}
}
The problem is when I get the person data from db the related user returns null even using the Include extension. FYI: I've tried to load the users from db using the dbcontext but it returns null too.