.netcore2.1 ef 使用外键关联查询
//实体类
[Table("invoiceinfo", Schema = "obs")] public class invoice { [Key] public string invoice_num { get; set; } public string merchant_id { get; set; } public DateTimeOffset? verify_time { get; set; } //one to many
public virtual IList<invoice_relation> invoice_file_list { get; set; } //one to one public virtual business invoice_business { get; set; } }
//数据库 PostgreDbContext相关配置
protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); builder.Entity<invoice>().HasMany(i => i.invoice_file_list).WithOne().HasForeignKey(f => f.invoice_num); builder.Entity<invoice>().HasOne(i => i.invoice_business).WithMany().HasForeignKey(i => i.merchant_id); }
//.file为invoice_file_list类中字段属性,.invoiced_party为invoice_business字段属性
IQueryable<invoice> invoiceList = _postgreDbContext.invoice.AsNoTracking().Include("invoice_file_list.file").Include("invoice_business.invoiced_party");