2021年2月5日星期五

ef core how to get data from different tables with foreign keys at once? (DB first)

I have two tables in my project, "educator" and "educator license".

The educator license is depended to the educator table with foreign key. (DB first)

Foreign keys with id in my db: (educator can have N license)

enter image description here

my educator model like:

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

my educator license model like:

public class EducatorLicense  {      [Key]      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]      public int id { get; set; }      [JsonIgnore]      public int EducatorId { get; set; }      public string LicenseType { get; set; }  }  

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

and I got that exception: enter image description here

I want it to getting the educator license table with it when getting the educator table.

I think I can do this at the business layer, by going to the database twice with lazy load.

Is there a way to getting two tables connected with foreign to each other at once?

https://stackoverflow.com/questions/66065027/ef-core-how-to-get-data-from-different-tables-with-foreign-keys-at-once-db-fir February 05, 2021 at 10:27PM

没有评论:

发表评论