none
Entity Framework 查询return结果相关联的类(外键)值为null RRS feed

  • 问题

  •  public class PrescriptionDBModel
        {
            public int ID { get; set; }
    
            public string OBID { get; set; }
    
            public virtual Prescription prescription { get; set; }
    
    
        }
    
      public class Prescription 
        {
            private int id;
            
            public int Id
            {
                get { return id; }
                set
                {
                    id = value;
                    RaisePropertyChanged("Id");
                }
            }
    
            /// <summary>
            /// 标题
            /// </summary>
            private string title;
    
            public string Title
            {
                get { return title; }
                set
                {
                    title = value;
                    RaisePropertyChanged("Title");
                }
            }
    
          
    
            /// <summary>
            /// 阶段List
            /// </summary>
            private ICollection<Phase> phaseList;
    
            public virtual ICollection<Phase> PhaseList
            {
                get { return phaseList; }
                set
                {
                    phaseList = value;
                    RaisePropertyChanged("PhaseList");
                }
            }
    
          
        }
    }
    
     public class Phase
        {
            public int ID { get; set; }
            public virtual ICollection<Stage> StageList { get; set; }
        }
    
        public class Stage : BindableBase
        {
            public int ID { get; set; }
            /// <summary>
            /// 阶段序号
            /// </summary>
            public int StageNum { get; set; }
    
    
            
        }
    
    public List<Prescription> GetDataList(string id)
            {
                try
                {
    
                    using (var db = new DBModel())
                    {
    
    
                        var dataList = db.PrescriptionDBModels.Include("PrescriptionInfo").Where(n => n.OBID == OBID).Select(m => m.PrescriptionInfo).ToList();
    
    
                        return dataList;
                    }
                  
    
                }
    
                catch (Exception ex)
                {
                    LogHelper.WriteErrorLog(ex);
                    throw;
                }
    
            }

    在using阈中,调试查看dataList的PhaseList属性是有值的,但return出来,到主函数中dataList的PhaseList就为空,我想是using结束后把这个外键属性类析构掉了,我把延迟加载也关了,还是一样为null,请问如何解决?


    2019年7月26日 11:10

答案

  • Hi nju_lin,

    你有尝试在Using语句外面声明变量dataList吗?如下,

        public List<Prescription> GetDataList(string id)
        {
            List<Prescription> dataList = new List<Prescription>();
            try
            {
                using (var db = new DBModel())
                {
                    dataList = db.PrescriptionDBModels.Include("PrescriptionInfo").Where(n => n.OBID == OBID).Select(m => m.PrescriptionInfo).ToList();
                    return dataList;
                }
            }
        }

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 nju_lin 2019年8月27日 6:35
    2019年7月29日 5:19

全部回复

  • Hi nju_lin,

    你有尝试在Using语句外面声明变量dataList吗?如下,

        public List<Prescription> GetDataList(string id)
        {
            List<Prescription> dataList = new List<Prescription>();
            try
            {
                using (var db = new DBModel())
                {
                    dataList = db.PrescriptionDBModels.Include("PrescriptionInfo").Where(n => n.OBID == OBID).Select(m => m.PrescriptionInfo).ToList();
                    return dataList;
                }
            }
        }

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 nju_lin 2019年8月27日 6:35
    2019年7月29日 5:19
  • 这样的话return出来的 dataList.PhaseList是有值,但是PhaseList中的Stage类为null, Phase与Stage是一对多的关系,请问怎么样才能提取出完整的PhaseList?
    2019年8月1日 12:01
  • 请教一下这个外键读取问题,谢谢
    2019年8月7日 7:39