簡體   English   中英

EF Core Identity - 與返回 null 的另一個實體具有一對一關系的應用程序用戶

[英]EF Core Identity - Applicationuser with one-to-one relationship with another entity returning null

我與 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; }
    }

數據庫上下文:

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"));           
             
        }
    }

人實體配置:

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);
         }
    }

問題是當我從 db 獲取人員數據時,即使使用 Include 擴展,相關用戶也會返回 null。 僅供參考:我嘗試使用 dbcontext 從 db 加載用戶,但它也返回 null。

我測試了你的代碼,我認為你的配置沒有問題,所以,問題可能是由你的數據插入引起的。你可以嘗試添加一個新的Person並嘗試像下面這樣找到它們:

  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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM