How to get FK table data when querying PK table in .Net core?

.net-core c# core entity-framework-core

Question

I have a primary key spe_manuscript_num in table SPE_COMMON_DATA and it is FK to other tables:

enter image description here

But the .NET Core model is generated by Scaffold-DbContext like this:

public partial class SpeCommonData
{     
    public string SpeManuscriptNum { get; set; }
    ..
    ..
    public virtual ICollection<SpeAttachment> SpeAttachment { get; set; }
    public virtual ICollection<SpeMiscData> SpeMiscData { get; set; }
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
       modelBuilder.Entity<SpeCommonData>(entity =>
        {
               ....
               //nothing about FK tables
        }
        modelBuilder.Entity<SpeMiscData>(entity =>
        {
            entity.ToTable("SPE_MISC_DATA");

            entity.Property(e => e.Id)
                .HasColumnName("id")
                .HasColumnType("numeric(38, 0)")
                .ValueGeneratedOnAdd();

            entity.Property(e => e.SpeManuscriptNum)
                .IsRequired()
                .HasColumnName("spe_manuscript_num")
                .HasMaxLength(32)
                .IsUnicode(false);
             ....
            entity.HasOne(d => d.SpeManuscriptNumNavigation)
                .WithMany(p => p.SpeMiscData)
                .HasForeignKey(d => d.SpeManuscriptNum)
                .HasConstraintName("FK__SPE_MISC___spe_m__02084FDA");
        });
     modelBuilder.Entity<SpeAttachment>(entity =>
        {
            entity.HasKey(e => e.AttachmentId)
                .HasName("PK__SPE_ATTA__B74DF4E2D1989600");

            entity.ToTable("SPE_ATTACHMENT");

            entity.Property(e => e.AttachmentId).HasColumnName("attachment_id");
            ...
            entity.Property(e => e.SpeManuscriptNum)
                .HasColumnName("spe_manuscript_num")
                .HasMaxLength(32)
                .IsUnicode(false);

            entity.HasOne(d => d.SpeManuscriptNumNavigation)
                .WithMany(p => p.SpeAttachment)
                .HasForeignKey(d => d.SpeManuscriptNum)
                .HasConstraintName("FK__SPE_ATTAC__spe_m__3EA749C6");
        });

My question is when I query table SPE_COMMON_DATA I am not able to pull other tables data based on FK I know nothing defined in modelBuilder.Entity<SpeCommonData>?

How to get other tables data also when query SPE_COMMON_DATA, like the ORM model?

enter image description here

enter image description here

1
0
4/6/2020 4:33:17 PM

Accepted Answer

Your entity should have reference to the entities that have FK relations. Then while querying you can use Include method of EF so that it queries and returns FK tables as well.

Can you check this link please docs.microsoft.com/en-us/ef/ef6/querying/related-data

0
4/6/2020 6:37:06 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow