如何通过外键引用访问表数据?

时间:2016-03-29 12:24:27

标签: c# mysql entity-framework linq

我在我的一个实体中有一个声明,它使用外键返回IEnumberable<CustomField>

我在我的存储库中使用了LINQ来测试下面的方法,看看它是否有效。但是当我在实体中使用外键引用时,它返回null。我在这里错过了什么吗?如何使用外键来访问另一个实体中的数据。

Invoice Entity:
 [Table("vwinvoice")]
    public class Invoice {
        [Key]
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
        public int Sys_InvoiceID { get; set; }
        [DisplayName("Inc.In Turnover")]
        public bool Turnover { get; set; }
        public int FK_StatusID { get; set; }
        [DisplayName("Invoice No.")]
        public string InvoiceNumber { get; set; }
        [DisplayName("Invoice Date")]
        public DateTime InvoiceDate { get; set; }
        [DisplayName("Document Type")]
        public string DocType { get; set; }
        [DisplayName("Supplier Invoice No.")]
        [Column("SupplierInvoiceNumber")]
        public string SuppInvNumber { get; set; }
        public int FK_SupplierID { get; set; }
        [DisplayName("Account Number")]
        public string AccountNumber { get; set; }
        [DisplayName("Order Number")]
        public string OrderNumber { get; set; }
        [DisplayName("Order Date")]
        public DateTime? OrderDate { get; set; }
        [DisplayName("Currency Code_Doc")]
        public string CurrencyCode_Doc { get; set; }
        [DisplayName("Net Amount_Doc")]
        public decimal? NetAmount_Doc { get; set; }
        [DisplayName("VAT Amount_Doc")]
        public decimal? VATAmount_Doc { get; set; }
        [DisplayName("Gross Amount_Doc")]
        [Required]
        public decimal? GrossAmount_Doc { get; set; }
        [DisplayName("Currency Code_Home")]
        public string CurrencyCode_Home { get; set; }
        [DisplayName("Net Amount_Home")]
        public decimal? NetAmount_Home { get; set; }
        [DisplayName("VAT Amount_Home")]
        public decimal? VATAmount_Home { get; set; }
        [DisplayName("Gross Amount_Home")]
        public decimal? GrossAmount_Home { get; set; }
        [DisplayName("Payment Reference")]
        public string PaymentReference { get; set; }
        [DisplayName("Supplier")]
        public string AccountName { get; set; }
        [DisplayName("Status")]
        public string StatusName { get; set; }
        [DisplayName("Auditor Comments")]
        public string AuditorComments { get; set; }
        [DisplayName("Reviewer Comments")]
        public string ReviewerComments { get; set; }
        [DisplayName("Data Source")]
        [Required]
        public string DataOrigin { get; set; }
        public int DetailLineCount { get; set; }

        public IEnumerable<CustomField> ClientData {
            get {
//Use the CustomFields foreign key to gain access to the data returns null.
                return GetCustomFieldData(this.CustomFields.Select(r => r));
            }
        }

        private IEnumerable<CustomField> GetCustomFieldData(IEnumerable<Entities.CustomFields> enumerable) {
            return (from f in enumerable
                    select new CustomField {
                        Name = f.FK_CustomHeader,
                        Value = f.Value
                    });
        }

        //Custom Field Additions
        public virtual ICollection<CustomFields> CustomFields { get; set; }
    }

自定义字段实体:

[Table("tblCustomFields")]
    public class CustomFields {
        [Key]
        public int ID { get; set; }

        public int? FK_SysInvoiceID { get; set; }

        [StringLength(255)]
        public string FK_CustomHeader { get; set; }

        [StringLength(255)]
        public string Value { get; set; }

        public virtual Invoice Invoices { get; set; }

        public virtual CustomFieldHeaders CustomFieldHeaders { get; set; }
    }

我也不能在get语句中放置一个断点来看看会发生什么,为什么会这样?每当我尝试返回发票清单时,它就会跳过断点,这可以在这里看到:

 public IQueryable<Invoice> Invoices {
            get {
                var x = _ctx.Invoices.ToList();
                return _ctx.Invoices;
            }
        }

1 个答案:

答案 0 :(得分:1)

您在声明virtual媒体资源时使用CustomFields关键字。因此它将是懒惰加载。如果您希望在从存储库返回后填充属性,则需要在方法中明确Include表:

var x = _ctx.Invoices.Include(i => i.CustomFields).ToList();
return _ctx.Invoices;

或者您可以删除virtual关键字,并且将始终填充该属性,随后是数据库连接的性能损失,并且只要您访问Invoices,就会返回额外的数据。