【发布时间】:2021-05-09 21:34:06
【问题描述】:
我的项目中有两个表,“教育者”和“教育者许可证”。
教师许可证依赖于具有外键的教师表。 (数据库优先)
我的数据库中带有 id 的外键: (教育者可以有N个执照)
我的教育者模型如下:
public class Educator
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[JsonIgnore]
public int id { get; set; }
[JsonIgnore]
public int CompanyId { get; set; }
public string PublicId { get; set; }
public string Name { get; set; }
public string PhoneNumber { get; set; }
public int Password { get; set; }
public bool Gender { get; set; }
public string AdminNote { get; set; }
public List<EducatorLicense> EducatorLicense { get; set; }
public bool Status { get; set; }
public TimestampsModel Timestamps { get; set; }
}
我的教育者许可模式,例如:
public class EducatorLicense
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
[JsonIgnore]
public int EducatorId { get; set; }
public string LicenseType { get; set; }
}
我的 DbContext:
protected override void OnModelCreating(ModelBuilder model)
{
model.Entity<Educator>(builder =>
{
builder.ToTable("educators");
builder.Property(p => p.id)
.ValueGeneratedOnAdd();
builder.OwnsOne(c => c.Timestamps,
a =>
{
a.Property(p => p.CreatedAt).HasColumnName("createdAt");
a.Property(p => p.UpdatedAt).HasColumnName("updatedAt");
});
builder.HasOne(d => d.EducatorLicense)
.WithMany()
.HasForeignKey(d => d.Id)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("educatorlicenses_educatorid_foreign");
});
model.Entity<EducatorLicense>(builder =>
{
builder.Property(p => p.id)
.ValueGeneratedOnAdd();
builder.HasKey(c => c.id);
});
}
我希望它在获取教育者表时也能获得教育者许可表。
我想我可以在业务层做到这一点,通过延迟加载两次访问数据库。
有没有办法让两个表同时连接到外部?
【问题讨论】:
标签: entity-framework .net-core entity-framework-core db-first