延迟加载不起作用C#

时间:2018-06-19 00:15:13

标签: c# entity-framework

我无法单独获取Persons类的现有数据库值。

我在这里提供了我的BO&实体模型。 ID是测试所有实体的主键,其他实体ID作为外键添加

BO:

using (var ctx = GetContext())
{
Test testOri = ctx.Tests.Where(p => p.ID == this.Test.ID).FirstOrDefault();             
if (testOri != null)
{
}

public partial class Test : EntityBase
{
    public bool Active { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Test()
    {
        Events = new HashSet<Event>();
        Persons = new Person();
    }
    [Key]
    [StringLength(20)]
    public string ID { get; set; }

    [Required()]
    [StringLength(50)]
    public string Details { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    [XLTargetName(null)]
    public virtual ICollection<Event> Events { get; set; }

    [XLTargetName(null)]
    public virtual Person Persons { get; set; }

    public override void AddToModel(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Test>().HasMany(e => e.Events).WithRequired(e => e.Test).WillCascadeOnDelete(false);
        modelBuilder.Entity<Test>().HasOptional(e => e.Persons).WithRequired(e => e.Test).WillCascadeOnDelete(false);
    }

}


public partial class Person : EntityBase
{
    public Person()
    {
        Active = true; //DEFAULT TO ACTIVE
    }

    [Key()]
    public string PersonID { get; set; }

    [Required()]
    [StringLength(20)]
    public string ID { get; set; }

    [Required()]
    [StringLength(50)]
    public string Name { get; set; }

    [XLTargetName(null)]
    [JsonIgnore]
    [ScriptIgnore]
    public virtual Test Test { get; set; }

    public override void AddToModel(System.Data.Entity.DbModelBuilder modelBuilder)
    {

    }
}


public partial class Events : EntityBase
{
    public Events()
    {
        Active = true; //DEFAULT TO ACTIVE
    }

    [Key()]
    public string EventID { get; set; }

    [Required()]
    [StringLength(20)]
    public string ID { get; set; }

    [Required()]
    [StringLength(50)]
    public string EventDetails { get; set; }


    [XLTargetName(null)]
    [JsonIgnore]
    [ScriptIgnore]
    public virtual Test Test { get; set; }

    public override void AddToModel(System.Data.Entity.DbModelBuilder modelBuilder)
    {

    }
}

根据测试ID,我能够获得测试和测试的现有记录。事件但无法获得Person的值。人员返回null。请协助解决相同问题,

0 个答案:

没有答案