.netcore2.1 ef 使用外键关联查询
程序员文章站
2024-01-12 08:56:16
//实体类 [Table("invoiceinfo", Schema = "obs")] public class invoice { [Key] public string invoice_num { get; set; } public string merchant_id { get; set... ......
//实体类
[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");