实体框架:延迟加载不会加载

时间:2019-01-13 00:35:53

标签: c# entity-framework model-view-controller lazy-loading

我正在尝试在ASP.NET上创建Web应用程序,但是遇到了问题。

填充数据库后,我无法检索对象的层次结构,相反,我得到的是“空”

我听说了使用.include.load进行加载的其他方法,但是我想尝试使用延迟加载。我尝试添加:

public MiningContext() {
    this.Configuration.LazyLoadingEnabled = true;
    this.Configuration.ProxyCreationEnabled = true;
}

所有外国字段都是虚拟的,类和构造函数是公共的。 我有下一个层次结构:帐户->角色->公司->联盟

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;


namespace EveMining.Models
{
       public class Account
    {
        public Account() { }
        public int Id { get; set; }


        public Character Character { get; set; }

        public string AuthToken { get; set; }

        public string RefreshToken { get; set; }

        public virtual DateTime LastUpdated { get; set; }

    }
    public class Character
    {
        public Character() { }
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        [Key]
        public int Id { get; set; }

        public virtual Corporation Corporation { get; set; }

        public string Name { get; set; }
        public string Image64 { get; set; }
        public string Image128 { get; set; }

    }
        public Corporation() { }
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        [Key]
        public int Id { get; set; }

        public virtual Alliance Alliance { get; set; }
        public string Name { get; set; }
        public string Image64 { get; set; }
        public string Image128 { get; set; }

    }
    public class Alliance
    {
        public Alliance() { }
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        [Key]
        public int Id { get; set; }

        public string Name { get; set; }
        public string Image64 { get; set; }
        public string Image128 { get; set; }

    }
}

当我尝试使用下一种测试方法从数据库中检索数据时:

List<Account> test = db.Accounts.ToList<Account>();

https://puu.sh/CvMIA/4feeb02c30.png

我得到了对象,但是他的孩子(字符)为空。

UPD:问题已解决,虚拟机放置在错误的位置!

0 个答案:

没有答案
相关问题