如何通过外键引用获得表数据的访问权?

vybvopom  于 2022-10-03  发布在  Mysql
关注(0)|答案(1)|浏览(109)

我的一个实体中有一条使用外键返回IEnumerable<CustomField>的语句。

我已经在我的存储库中使用了LINQ来测试下面的方法,看看它是否工作,它确实工作了。但是,当我在实体中使用外键引用时,它返回NULL。我是不是漏掉了什么?如何使用外键访问另一个实体中的数据。

发票主体:

[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; }
}

CustomFields实体:

[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;
    }
}
kmbjn2e3

kmbjn2e31#

声明CustomFields属性时使用的是virtual关键字。因此,它将是延迟加载的。如果希望在从存储库返回后填充该属性,则需要在方法中显式地Include该表:

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

或者,您可以删除virtual关键字,该属性将始终被填充,从而影响数据库联接的性能,并在您访问Invoices时返回额外数据。

相关问题