表 dbo.Users:用户 ID 、用户名、电子邮件、...
表 dbo.Client: 用户 ID、名字、姓氏、...
楷模:
public class User
{
public long UserId { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public Client Client { get; set; }
...
}
public class Client
{
public long UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
...
}
我的 DBContents 包含:
public DbSet<User> Users { get; set; }
public DbSet<Client> Clients { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.HasKey(x => x.UserId)
.HasRequired(x => x.Client).WithRequiredPrincipal();
modelBuilder.Entity<Client>()
.HasKey(x => x.UserId)
.ToTable("Client");
}
结果我有带有空客户端的用户模型。但是,如果我通过相同的 UserId 获取 Client,我会看到该模型已填充。
怎么了?
编辑:
public virtual Client Client { get; set; }