0

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.

Yinqiu
  • 6,609
  • 1
  • 6
  • 14
  • Trying adding `virtual` to `public Person Person { get; set; }`. For more info read this answer https://stackoverflow.com/questions/41881169/navigation-property-should-be-virtual-not-required-in-ef-core – Kosala W Jan 15 '21 at 00:33

1 Answers1

0

I tested your code,I think there is no problem with your configuration,so,the issue may caused by your data insert.You can try to add a new Person and try to find them like bellow:

  var user = new ApplicationUser
        {
            Email = "www.example.com"
        };
        var p = new Person
        {
            FirstName = "AA",
            //...
            User = user,  
        };
        _context.Persons.Add(p);
        _context.SaveChanges();

        var u =  _context.User.ToList();
        var pe = _context.Persons.Include(c => c.User).ToList();
Yinqiu
  • 6,609
  • 1
  • 6
  • 14