Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

.net core - Table relationships in EFCore are not included in query

I am trying to run this query: var test = (await _db.TableA.Include(c=> c.TableB).Include(c=>c.TableC).ToListAsync(); The models are as such:

[Serializable]
[Table("TableA")]
public class TableA
{
    [Key]
    public Guid TableASpecialIdentifier { get; set; }

    public ICollection<TableB> TableB { get; set; }
    public TableC TableC{ get; set; }
}

[Serializable]
[Table("TableB")]
public class TableB
{
    [Key]
    public Guid TableBSpecialIdentifier { get; set; }
    public Guid TableASpecialIdentifier { get; set; }

    [ForeignKey("TableASpecialIdentifier")]
    public TableA TableA { get; set; }
}

[Serializable]
[Table("TableC")]
public partial class TableC
{
    [Key]
    public Guid TableCSpecialIdentifier { get; set; }
    public Guid? TableASpecialIdentifier { get; set; }

    [ForeignKey("TableASpecialIdentifier")]
    public TableA TableA { get; set; }
}

When I run it as noted above, nothing comes back. If I run var test = (await _db.TableB.Include(c=> c.TableA).ToListAsync(); I get results, same with var test = (await _db.TableC.Include(c=> c.TableA).ToListAsync(); producing results as well. But trying to include anything into TableC doesnt work. I have also tried removing the Data Annotations and use the Fluent API like:

            modelBuilder.Entity<TableB>()
                .HasKey(c => c.TableBSpecialIdentifier);

            modelBuilder.Entity<TableA>()
                .HasKey(f => f.TableASpecialIdentifier);

            modelBuilder.Entity<TableC>()
                .HasKey(c => c.TableCSpecialIdentifier);

            modelBuilder.Entity<TableB>()
                .HasOne(c => c.TableA)
                .WithMany(g => g.TableB)
                .HasForeignKey(s => s.TableASpecialIdentifier);

            modelBuilder.Entity<TableA>()
                .HasOne(i => i.TableC)
                .WithOne(c => c.TableA)
                .HasForeignKey<TableC>(f => f.TableASpecialIdentifier);

This has the same end result as before. So I tried writing it in LINQ with similar results:

var test = (from A in _db.TableA
                        join C in _db.TableC on C.TableASpecialIdentifier equals A.TableASpecialIdentifier
                        join B in _db.TableB on A.TableASpecialIdentifier equals B.TableASpecialIdentifier
                        select C).ToListAsync();

I'm not receiving any errors. This is what I'm trying to accomplish written in SQL:

Select c.* from TableA a 
   join TableB b on a.TableASpecialIdentifier = b.TableASpecialIdentifier 
   join TableC c on a.TableASpecialIdentifier = c.TableASpecialIdentifier 

I'm using .net core and EFcore. Any ideas on what I'm doing wrong? Sorry in advance for the crummy table names!

question from:https://stackoverflow.com/questions/65927722/table-relationships-in-efcore-are-not-included-in-query

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to run your query you have to put TableC key as a foreign key of TableA:

public class TableA
{
    [Key]
    public Guid TableASpecialIdentifier { get; set; }

    public Guid TableCSpecialIdentifier { get; set; 

    public ICollection<TableB> TableB { get; set; }
   
   [ForeignKey("TableCSpecialIdentifier")]
    public TableC TableC{ get; set; }
}


public partial class TableC
{
    [Key]
    public Guid TableCSpecialIdentifier { get; set; }
   
     [InverseProperty(nameof(TableA.TableC))]
     public virtual ICollection<TableA> TableAs { get; set; }
}

dbcontext:

modelBuilder.Entity<TableA>(entity =>
{
                     entity.HasOne(d => d.TableC)
                    .WithMany(p => p.TableA)
                    .HasForeignKey(d => d.TableCSpecialIdentifier)
                    .OnDelete(DeleteBehavior.ClientSetNull);
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...